4.2_控制流2while__range语句.docx
4.2_控制流2while__range语句.docx
1. while语句
Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:
while判断条件:执行语句……
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
The count is:0The count is:1The count is:2The count is:3The count is:4The count is:5The count is:6The count is:7The count is:8Good bye!
guess = int(input('Enter an integer : '))
print('No, the number is higher than that, keep guessing')
# You can do whatever you want in a block ...
print('No, the number is a lower than that, keep guessing')
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
2. range语句
print("you have only 3 chances to guess")
for i in range(1, num_chances + 1):
guess = int(input('Enter an integer : '))
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
# You can do whatever you want in a block ...
🐳 作者:hiszm 📢 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,万分感谢。 💬 留言:同时 , 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |