Python编程:从入门到实践读书笔记-7 用户输入和while循环

最近搬家+做新项目,导致没有足够的时间进行python学习了,搬到新地方,没有在西工大学习效率高,要学会适应!

7.2 while 循环简介
     for循环:针对集合中的每个元素都同一个代码块
     while循环:不断运行,知道指定的条件不满足为止。
7.2.1 使用while循环
     eg:
          current_number = 1
          while current_number <= 5:
               print(current_number)
               current_number += 1
     op:
          1
          2
          3
          4
          5
7.2.2 让用户选择何时退出
    情景1:玩重复游戏,直到输入quit时,游戏停止。
     eg:
          prompt = "\nTell me something, and I will repeat it back to you:"
          prompt += "\nEnter 'quit' to end the program."
          message = ""
          while message != 'quit':
               message = input(prompt + "\n")
               if message != 'quit':
                    print(message)
     op:
          Tell me something, and I will repeat it back to you:
          Enter 'quit' to end the program.
          Hello again.
          Hello again.
          Tell me something, and I will repeat it back to you:
          Enter 'quit' to end the program.
          quit
7.2.3 使用标志
      在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于 活动状态。
     这个变量被称为标志,充当了程序的交通信号灯。
     情景2:在情景1的基础上,如果输入有give up,游戏也依然结束。
     eg:
          prompt = "\nTell me something, and I will repeat it back to you:"
          prompt += "\nEnter 'quit' or 'give up'to end the program."
          active = True
          while active:
               message = input(prompt+ "\n")
               if message == 'quit' or message == 'give up': #多条件满足一个即终止游戏
                    active = False
               else:
               print(message)
     注意:
               1. 和情景1相比,此程序由于设置active = True,最初即处于活动状态,简化了while语句;
               2. 在情景1,将条件测试直接放在while语句 中,情景2,使用了一个标志来指出程序是否处于活动状态;
               3. 在复杂的程序中, 如很多事件都会导致程序停止运行的游戏中,标志很有用:在其中的任何一个事件导致活动标              
                   志 变成False时,主游戏循环将退出,此时可显示一条游戏结束消息,并让用户选择是否要重新玩。
7.2.4 使用break退出循环
     要立即退出while循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用 break语句。
     break语句用于控制程序流程,即控制哪些代码行将执行,哪些代码行不执 行,从而让程序按要求执行需要执行的代码。
     eg:
          prompt = "\nPlease enter the name of a city you have visited:"
          prompt += "\n(Enter 'quit' when you are finished.) "
          while True:
               city = input(prompt + "\n")
               if city == 'quit':
                    break
               else:
                    print("I'd love to go to " + city.title() + "!")
     注意:1. 以while True打头的循环将不断运行,直到遇到break语句。
               2. 在任何Python循环中都可使用break语句。例如,可使用break语句来退出遍历列表或字典 的for循环。
7.2.5 在循环中使用continue
     要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句
     情景1:从1到10,只打印奇数。
     eg:
          current_number = 0
          while current_number < 10:
               current_number += 1
               if current_number % 2 == 0:
                    continue
               print(current_number) 
     op:
          1
          3
          5
          7
          9
     解释:if语句检查current_number 与2的求模运算结果。结果为0(意味着current_number可被2整除)就执行continue语句, 忽略 continue后的代码,且返回到循环的开头。如果当前的数字不能被2整除,就执行循环中 余下的代码,Python将这个数字打印出来。
7.2.6 避免无限循环
     每个while循环都必须有停止运行的途径,这样才不会没完没了地执行下去。
     eg:
          x = 1
          while x <=5:
               print(x)
     注意:如果程序陷入无限循环,可按Ctrl + C,也可关闭显示程序输出的终端窗口。

练习:
     7.4
requested_topping = "Please select the topping you want:" + "\n"
requested_topping += "you can enter 'quit' to finish your request"
message = ''
while message != 'quit':
     message = input(requested_topping + "\n")
     if message != 'quit':
          print("We will take " + message + " in your Pisa" + "\n")
     7.5&7.6
          message = "Please enter your age:" + "\n"
message += "you can enter 'quit' when you are finished"
active = True
while active:
    age = input(message + "\n")
    if age == 'quit':
        active = False
    else:
        age = int(age)
        if age < 3:
            print("You are free to go to the movies ")
        elif age < 12:
            print("You should pay 10$ to go to the movies")
        else:
            print("You should pay 15$ to go to the movies")


     

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值