《Python编程从入门到实践》——学习python的第十天

这、python学习第十天

前言

今天是python学习的第十天,之前忘记更新了,所以很抱歉,这不来不上了嘛。

while循环

使用break语句来推出while循环

break可以直接控制那些代码运行,那些代码不运行,任何循环中都可以使用

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.)"


while True:
    city = input(prompt)
    if city == 'quit':
        break#使用break来终止循环,其实在c语言中已经学过了
    else:
        print(f"I'd love to go to {city.title()}!")

结果:
Please enter the name of a city you have visited:
(Enter ‘quit’ when you are finished.)xian
I’d love to go to Xian!

Please enter the name of a city you have visited:
(Enter ‘quit’ when you are finished.)zhengzhou
I’d love to go to Zhengzhou!

Please enter the name of a city you have visited:
(Enter ‘quit’ when you are finished.)quit

在循环中使用continue

要返回循环开头,并根据条件测试结果决定是否继续执行循环可使用continue语句

current_number = 0
while current_number < 10:#因为数字小于10所以所以进入while循环
    current_number+=1#每循环一次加一
    if current_number % 2 == 0:#如果该数为偶数,则进行continue程序,从而继续进行循环,不管偶数了
        continue


    print(current_number)#打印每次的奇数

结果:
1
3
5
7
9

避免无限循环
每个while循环都必需要有停止运行的途径,这样才不会没完没了地执行下去,所以必须设置好条件。

使用while循环处理列表和字典

通过while循环将字典和列表循环使用,可收集储存并组织大量输入,供以后查看和显示

在列表间移动元素

#首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice','brian','candace']#未验证用户
confirmed_users = []#已验证用户中为空


#验证每个用户,知道没有未验证的用户为止
#将每个已验证的用户都移到已验证的用户列表中


while unconfirmed_users:#只要列表不为空,就是True,就使得while一直循环下去
    current_user = unconfirmed_users.pop()#这里使用pop方法来进行删除和利用,删除末尾的,加到current——user中


    print(f"Verifying user:{current_user.title()}")
    confirmed_users.append(current_user)#同时多confired_users,进行扩充


    #显示所有已验证的用户
print(f"\nThe following users have been cofirmed:")
for confirmed_user in confirmed_users:
        print(confirmed_user.title())

结果:
Verifying user:Candace
Verifying user:Brian
Verifying user:Alice

The following users have been cofirmed:
Candace
Brian
Alice

删除为特定值的所有列表元素

这里使用了函数remove来删除所有为‘cat’的元素,

pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)


while 'cat' in pets:#每循环一次就删除一次‘cat’元素,知道全部被删除完
    pets.remove('cat')


print(pets)

结果:
[‘dog’, ‘cat’, ‘dog’, ‘goldfish’, ‘cat’, ‘rabbit’, ‘cat’]
[‘dog’, ‘dog’, ‘goldfish’, ‘rabbit’]

使用用户输入来填充字典

responses = {}#建立一个空白字典


#设置一个标志,指出调查是否继续
polling_active = True


while polling_active:
#提示输入被调查者的名字和回答。
    name = input("\nWhat's your name?")
    response = input("\nWhich mountain would you like to climb someday?")


#将回答导入字典中
    responses[name] = response#空白字典添加信息


#看看是否还有人要参与调查。
    repeat = input("would you like to let another person respond?(yes/no)")#这里把结束循环的权限交给了用户
    if repeat == 'no':
        polling_active = False#改变标志


#调查结束,显示结果
print(f"\n--Poll Results ---")
for name,repose in responses.items():#遍历字典不能丢items()
    print(f"{name} would like to climb {response}.")

结果:
What’s your name?yinyang

Which mountain would you like to climb someday?珠穆朗玛峰(输入)
would you like to let another person respond?(yes/no)no(输入)

–Poll Results —
yinyang would like to climb 珠穆朗玛峰.

函数

为什么需要函数?
函数可以多次执行同一项任务,避免了繁杂且重复的代码。

定义函数

def greet_user():#使用def来定义函数,向python指出了函数名,记得加上冒号
    """显示简单的语言"""#这里是文档字符串用于解释函数的作用
    print("Hello! Python world")#函数的工作内容
greet_user()#python 运行函数

结果:
Hello! Python world

函数的更多信息,咱们放到下一次来讲。

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值