python while循环语句例子
while 循环语句是在特定的条件下,重复执行一系列指令的程序控制结构。
一般格式:
while 条件:
statement(s)
例子1:输出0~10的数字
#!/usr/bin/python
# The initial value of the counter
counter = 0
# Execute the code as long as the condition (counter <= 10) is true
while counter <= 10:
print(counter)
# Increment the counter for the next iteration
counter = counter + 1
输出:
0
1
2
3
4
5
6
7
8
9
10
例子2:猜数游戏
# Ask user to guess the number
guess = int(input("Guess the number: "))
# Initial sum is 0
sum = 0
输出:
The sum is 5050
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论