python基礎實戰之猜年齡遊戲

python基礎實戰之猜年齡遊戲

1、簡單猜一次年齡

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('傻孩,年齡都輸很差')

2、能夠猜三次年齡

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('傻孩,年齡都輸很差')

3、能夠猜屢次年齡

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

4、最終版

要求:git

  • 能夠進行抽獎
  • 給定年齡(隨機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
相關文章
相關標籤/搜索