本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理
本篇文章來自騰訊云 作者:孤獨的明月
( 想要學習Python?Python學習交流群:1039649593,滿足你的需求,資料都已經上傳群檔案流,可以自行下載!還有海量最新2020python學習資料, )
閱讀本文需要2分鐘
Python基礎實戰之猜年齡游戲
給定年齡,用戶可以猜三次年齡
年齡猜對,讓用戶選擇兩次獎勵
用戶選擇兩次獎勵后可以退出
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 恭喜你獲得獎品: 變形金剛
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/238898.html
標籤:Python
