zoukankan      html  css  js  c++  java
  • 用python代码编写的猜年龄小游戏

    0. 奖励物品存放在文件price.txt

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

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

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

    import random
    
    age = random.randint(18, 60)  # 随机一个数字,18-60岁
    print(age)
    count = 0  # 计数
    
    f = open('price.txt', 'r', encoding='utf8')  # price.txt右下角为什么编码,则encoding为什么编码
    price_dict = f.read()
    price_dict = eval(price_dict)  # type:dict # 获取奖品字典
    f.close()
    
    price_self = dict()
    
    while count < 3:
        count += 1
        inp_age = input('请输入你想要猜的年龄:')
    
        # 判断是否为纯数字
        if not inp_age.isdigit():
            print('搞事就骂你傻逼')
            continue
    
        inp_age = int(inp_age)
    
        # 筛选年龄范围
        if inp_age > 60 or inp_age < 18:
            print('好好题目,18-60岁,非诚勿扰')
            continue
     
        # 核心逻辑
        if age == inp_age:
            print('猜中了,请选择你的奖品')
    
        # 打印商品
        for k, v in price_dict.items():
            print(f'奖品编号:{k} {v}')
    
        # 获取奖品的两次循环
        for i in range(2):
            price_choice = input('请输入你需要的奖品编号:')
    
            if not price_choice.isdigit():
                print("恭喜你已经获得一次奖品,奖品为空!并且请输入正确的奖品编号!")
                continue
    
            price_choice = int(price_choice)
    
            if price_choice not in price_dict:
                print('你想多了吧!')
            else:
                price_get = price_dict[price_choice]
                print(f'恭喜中奖:{price_get}')
    
                if price_self.get(price_get):
                    price_self[price_get] += 1
                else:
                    price_self[price_get] = 1
    
        print(f'恭喜你获得以下奖品:{price_self}')
            break
    
        elif age > inp_age:
            print('猜小了')
        elif age < inp_age:
            print('猜大了')
    
    continue
    
  • 相关阅读:
    Multidimensional Arrays
    TortoiseGit
    Excel教程(14)
    Excel教程(13)
    Excel教程(12)
    Excel教程(11)
    lock
    reflect
    game
    html x
  • 原文地址:https://www.cnblogs.com/asyouwish/p/11321172.html
Copyright © 2011-2022 走看看