题目
编写一个猜数游戏程序:
计算机随机生成一个100以内的正整数
用户通过键盘输入数字,猜测所生成的随机数
要进行次数限制
代码
import random
num = random.randint(1,100)
try_time = 7
while try_time >= 1:
input_num = input('Please guess an integer between 1 and 100:')
if not input_num.isdigit():
print('Please input integer!')
elif int(input_num) <= 0 or int(input_num) >= 100:
print('The number should between 1 and 100!')
else:
try_time -= 1
if int(input_num) == num:
print('Your guess is right!')
break
else:
if try_time > 0:
if int(input_num) > num:
print('Your guess is bigger. Please guess again, you still can guess',try_time,'times.')
else:
print('Your guess is smaller. Please guess again, you still can guess',try_time,'times.')
else:
print('Your guess is wrong. You have run out of tries. You cannot guess any more!')
C++实现猜数字小游戏:链接
|