Python基礎實戰之猜年齡遊戲

1、Python基礎實戰之猜年齡遊戲

  1. 給定年齡,用戶能夠猜三次年齡git

  2. 年齡猜對,讓用戶選擇兩次獎勵code

  3. 用戶選擇兩次獎勵後能夠退出遊戲

age = 18  # 答案
count = 0  # 遊戲次數控制
prize_dict = {0: '布娃娃', 1: '變形金剛', 2: '奧特曼', 3: '<Python從入門到放棄>'}

# 核心代碼
while count < 3:
    inp_age = input('請輸入你的年齡>>>')  # 與用戶交互

    # 判斷用戶是否騷擾(超綱:判斷用戶輸入的是否爲數字)
    if not inp_age.isdigit():
        print('傻逼,你的年齡輸錯了')
        continue

    inp_age_int = int(inp_age)

    # 核心邏輯,判斷年齡
    if inp_age_int == age:
        print('猜對了')

        print(prize_dict)  # 打印獎品

        # 獲取兩次獎品
        for i in range(2):
            prize_choice = input(
                '請輸入你想要的獎品,若是不想要,則輸入"n"退出!!!')  # 與用戶交互獲取獎品

            # 判斷是否須要獎品
            if prize_choice != 'n':
                print(f'恭喜你得到獎品: {prize_dict[int(prize_choice)]}')
            else:
                break
        break

    elif inp_age_int < age:
        print('猜小了')

    else:
        print('猜大了')

    count += 1  # 成功玩一次遊戲

    if count != 3:
        continue

    again_choice = input('是否繼續遊戲,繼續請輸入"Y",不然任意鍵直接退出.')  # 交互是否再一次

    # 判斷是否繼續
    if again_choice == 'Y':
        count = 0
請輸入你的年齡>>>18
猜對了
{0: '布娃娃', 1: '變形金剛', 2: '奧特曼', 3: '<Python從入門到放棄>'}
請輸入你想要的獎品,若是不想要,則輸入"n"退出!!!0
恭喜你得到獎品: 布娃娃
請輸入你想要的獎品,若是不想要,則輸入"n"退出!!!1
恭喜你得到獎品: 變形金剛
相關文章
相關標籤/搜索