结果位:5050
遇到break 就退出
计算1-一百偶数和
total = 0
i = 1
while i <= 100:
if i % 2 == 0:
total += i
i += 1
else:
i += 1
print(total)
结果:2550
或者:
total = 0
i = 0
while i <= 100:
total += i
i += 2
print(total)
结果:2550
猜数字游戏;1-100随机生成,猜对了就退出,大了就提示大了,小了就提示小了。
import random
num = random.randint(1, 100)
while 1:
number = int(input("请输入猜测数字:"))
if num == number:
print("恭喜!")
break
elif num > number:
print("猜小了!")
else:
print("猜大了!")