设定目标值为20,允许最多猜三次,对与否都会退出程序:
<pre>num = 20
count = 0
while count < 3:
guess_num = int(input('age:'))
if guess_num == num:
print('you got it !')
break
elif guess_num > num:
print('猜大了!')
else:
print('猜小了!')
count = count + 1
若要三次之后仍能继续,加入如下代码:
if count == 3:
countine_confirm = input('do you want to keep guessing...?')
if countine_confirm != 'n':
count = 0
else:
print('再接再厉!')</pre>