python猜年龄猜三次_python基础实战之猜年龄游戏

python基础实战之猜年龄游戏

一、简单猜一次年龄

age = 18

inp_age = input('请输入年龄>>>').strip()

if inp_age.isdigit():

inp_age = int(inp_age)

if age > inp_age:

print('猜小了')

elif age < inp_age:

print('猜大了')

else:

print('猜对了')

else:

print('傻孩,年龄都输不好')

二、可以猜三次年龄

age = 18

for i in range(3):

inp_age = input('请输入年龄>>>').strip()

if inp_age.isdigit():

inp_age = int(inp_age)

if age > inp_age:

print('猜小了')

elif age < inp_age:

print('猜大了')

else:

print('猜对了')

break

else:

print('傻孩,年龄都输不好')

三、可以猜多次年龄

age = 18

count = 0

tag = True

while tag:

count += 1

inp_age = input('请输入猜测年龄>>>').strip()

if inp_age.isdigit():

inp_age = int(inp_age)

if age > inp_age:

print('猜小了')

elif age < inp_age:

print('猜大了')

else:

print('猜对了')

break

else:

print('傻孩,年龄都输不好')

if count == 3:

choice = input('是否继续猜测,继续请按Y or y ,任意键退出').strip().lower()

if choice != 'y':

tag = False

else:

continue

四、最终版

要求:

可以进行抽奖

给定年龄(随机18-60),用户可以猜三次年龄

年龄猜对,让用户选择两次奖励

用户选择两次奖励后可以退出

import random #导入随机库

prize_dic = {0: '气球', 1: '女朋友', 2: '劳斯莱斯', 3: '宝马', 4: '牛逼', 5: '坦克', 6: '大炮', 7: '飞机'} # type:dict # 奖品单

user_price_dic = {} # type:dict

age = random.randint(18,19) # 让年龄随机18或者19

count = 0

while count<3:

count+=1

inp_age = input('请输入猜测的年龄>>').strip()

if not inp_age.isdigit():

print('输入错误,请输入数字')

continue

inp_age= int(inp_age)

inp_age = int(inp_age)

if age > inp_age:

print('猜小了')

elif age < inp_age:

print('猜大了')

else:

print('猜对了')

for k, v in prize_dic.items():

print(k, v)

for i in range(2):

choice_prize = input('请输入奖品编号>>>').strip()

if not choice_prize.isdigit():

print('撒掉,一边弯曲')

continue

choice_prize = int(choice_prize)

prize = prize_dic[choice_prize]

print('获得了', prize)

if prize not in user_price_dic:

user_price_dic[prize] = 1

else:

user_price_dic[prize] += 1

print('奖品如下', user_price_dic)

break

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值