Loading

python控制语句_continue break的使用

example:

 1 while True
 2      print("Who are you?")
 3      name = input()
 4      if name != 'Joe':
 5          continue
 6      print("Hello,Joe. What is the password? (It sis a fish.)")
 7      password = input()
 8      if password == 'swordfish':
 9          break
10 print("Access granted.")

  • 行号4、如果用户输入的名字不是Joe
  • 行号5、continue语句将导致程序执行跳回到循环开始处。再次对条件求值是,执行总是进入循环,因为条件就是True。
  • 行号7、如果执行通过了if语句,用户就被要求输入口令。
  • 行号8/9、如果输入的口令是swordfish,break语句运行,执行跳出while循环,打印Access granted。否则执行继续while循环的末尾,又跳回到循环的开始
posted @ 2022-08-06 13:55  solomon-zj  阅读(29)  评论(0)    收藏  举报