方法一:循环猜知道猜对
secret = 24 #设定秘密数字
while True:
guess = input('你来猜猜我的秘密数字是多少:') #输入猜测数字
if int(guess)==secret: #数字对比
print('正确!你很棒哦。')
break
elif int(guess)>secret:
print('你猜的太大了,请重新猜猜~')
else:
print('你猜的太小了,请重新猜猜~')
方法二:只猜3次-for语句
secret = 24
for i in range(3):
guess = input('guess which number is my secret:')
if int(guess) ==secret:
print('正确!你很棒哦。') #输出结果
break
elif int(guess)>secret:
print('你猜的太大了,请重新猜猜~')
else:
print('你猜的太小了,请重新猜猜~')
else:
print('给你3次机会都猜不到,你失败了。')
while True:
learn()
网友评论