Python基础实战之猜年龄游戏
一、Python基础实战之猜年龄游戏
- 给定年龄,用户可以猜三次年龄
- 年龄猜对,让用户选择两次奖励
- 用户选择两次奖励后可以退出
age = 18 #给定的答案
count = 0 #猜的次数
prize_dict = {0: '奥特曼', 1: 'book', 2: 'Python从入门到放弃'} #奖品使用字典格式
#核心代码
while count < 3:
input_age = input("输入你要猜的年龄:") #与用户交互
if not input_age.isdigit(): #判断用户输入内容是否为数字
print("沙雕,输入格式错误!")
continue
input_age = int(input_age)
if input_age == age:
print('猜对了!')
print(prize_dict)
#for循环控制获得两次奖品
for i in range(2):
prize_choice = input('输入你想要的奖品,如果不要,请输入‘no’退出')
#判断是否需要奖品
if prize_choice != 'no':
print(f'恭喜你获得奖品{prize_dict[int(prize_choice)]}')
else:
break
break
elif input_age < age:
print('猜小了!')
else:
print('猜大了!')
count += 1 #成功完成一次游戏
if count != 3:
continue
#以下与用户交互,判断是否继续进行游戏
again_choice = input("是否继续游戏,如果继续请输入‘yes’,否则任意键退出!")
if again_choice == 'yes':
count = 0

浙公网安备 33010602011771号