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



posted @ 2019-08-01 15:50  豆瓣酱瓣豆  阅读(441)  评论(0编辑  收藏  举报