|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
作业:给予三次机会,猜1-10的数字,若猜三次仍然不对,则结束游戏。
代码如下:
import random
secret=random.randint(1,10)
times=3
guess=0
print('猜一个数:',end='')
while (guess!=secret) and (times > 0):
guess=input()
num=int(guess)
times=times-1
if num==secret:
print('guess right')
print('no reward')
else:
if num > secret:
print('too large')
else:
print('too small')
if times >0:
print('try again:',end='')
else:
print('used up your chances')
print(' the end of the game')
我在运行过程中,发现只有运行三次都错,才会出现最后一行: the end of the game
只要三次以内猜对,则不会运行最后一行 print(' the end of the game')
因为最后一行代码是和while并列的代码,所以我原本想的是,只要结束游戏,就会出现最后一行
不知道哪里出现了问题?感谢
|
|