您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關使用python怎么制作一個猜數字游戲,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
import random import time
我們用變量與input實現
name = str(input('請輸入用戶名:')) print('歡迎您,'+name)
這里我們得用except制作亂輸文本就游戲結束的程序
亂輸文本就結束
try: age = int(input('請輸入年齡:')) except ValueError: print('非法輸入') age = 30000
順便把年齡設為30000[滑稽]
再根據年齡大小分配金幣
if age <10: gold = 500 print('您的初始金幣為500') if age <20 and age >10: gold = 1000 print('您的初始金幣為1000') if age >20 and age <30: gold = 1500 print('您的初始金幣為1500') if age >30 and age <1000: gold = 200 print('年齡過大,您的初始金幣為200')
用if語句保證金幣小于10000,如果等于10000,游戲結束
if gold !=10000: Game_start = str(input('是否開始游戲?(True或False)')) while gold !=10000:
接下來就是最重要的游戲部分了,認真!
我們還得用if語句判斷Game_start變量為True,并創建一個隨機數列表
if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
然后設定回答變量和正確答案與錯誤答案
answer = str(input('請猜數(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big'
還得用if語句判斷正確、錯誤與非法輸入的答案
if answer ==result: print('您贏了!') gold = gold + 100 print('隨機數為'+str(list_123)) print('您現在有'+str(gold)+'金幣') elif answer ==error_result: print('您輸了...') print('隨機數為'+str(list_123)) gold = gold - 100 print('您現在有'+str(gold)+'金幣') else: print('非法輸入') gold = 10000
再制作購買道具的模塊
太長了我懶得寫一句一句解析
if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您現在可以購買道具,是否購買?(True或False):')) if answer =='True': print('請說出要購買der道具') print('*'*41) print('*金幣翻倍器[現有的] 金幣翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('請說出要翻der倍數:')) gold = str(gold * answer) print('您現在有'+gold+'金幣') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('請說出要翻der倍數:')) gold_mang = 100 * answer gold = gold + gold_mang print('您現在有'+str(gold)+'金幣') gold = int(gold) else: print('沒有這個道具,游戲結束') gold = 10000 elif answer =='False': print('繼續游戲') else: print('非法輸入') gold = 10000
我寫到一半發現沒有把上面的橋接上,所以現在來橋接
import random import time name = str(input('請輸入用戶名:')) print('歡迎您,'+name) try: age = int(input('請輸入年齡:')) except ValueError: print('非法輸入') age = 30000 gold = 10000 if age <10: gold = 500 print('您的初始金幣為500') if age <20 and age >10: gold = 1000 print('您的初始金幣為1000') if age >20 and age <30: gold = 1500 print('您的初始金幣為1500') if age >30 and age <1000: gold = 200 print('年齡過大,您的初始金幣為200') if gold !=10000: Game_start = str(input('是否開始游戲?(True或False)')) while gold !=10000: if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] answer = str(input('請猜數(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big' if answer ==result: print('您贏了!') gold = gold + 100 print('隨機數為'+str(list_123)) print('您現在有'+str(gold)+'金幣') elif answer ==error_result: print('您輸了...') print('隨機數為'+str(list_123)) gold = gold - 100 print('您現在有'+str(gold)+'金幣') else: print('非法輸入') gold = 10000 if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您現在可以購買道具,是否購買?(True或False):')) if answer =='True': print('請說出要購買der道具') print('*'*41) print('*金幣翻倍器[現有的] 金幣翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('請說出要翻der倍數:')) gold = str(gold * answer) print('您現在有'+gold+'金幣') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('請說出要翻der倍數:')) gold_mang = 100 * answer gold = gold + gold_mang print('您現在有'+str(gold)+'金幣') gold = int(gold) else: print('沒有這個道具,游戲結束') gold = 10000 elif answer =='False': print('繼續游戲') else: print('非法輸入') gold = 10000
其實到這里我們就基本完成了,但如果只寫到這里會有很多bug,不想看了也可以就玩這個版本
還剩了一些代碼,我也懶得寫了,直接完整代碼
import random import time name = str(input('請輸入用戶名:')) print('歡迎您,'+name) try: age = int(input('請輸入年齡:')) except ValueError: print('非法輸入') age = 30000 gold = 10000 if age <10: gold = 500 print('您的初始金幣為500') if age <20 and age >10: gold = 1000 print('您的初始金幣為1000') if age >20 and age <30: gold = 1500 print('您的初始金幣為1500') if age >30 and age <1000: gold = 200 print('年齡過大,您的初始金幣為200') if gold !=10000: Game_start = str(input('是否開始游戲?(True或False)')) while gold !=10000: if Game_start =='True': list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] while list_123 ==10: list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),] answer = str(input('請猜數(big或small):')) if list_123[0] + list_123[1] + list_123[2] >10: result = 'big' error_result = 'small' if list_123[0] + list_123[1] + list_123[2] <10: result = 'small' error_result = 'big' if answer ==result: print('您贏了!') gold = gold + 100 print('隨機數為'+str(list_123)) print('您現在有'+str(gold)+'金幣') elif answer ==error_result: print('您輸了...') print('隨機數為'+str(list_123)) gold = gold - 100 print('您現在有'+str(gold)+'金幣') else: print('非法輸入') gold = 10000 if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000: answer = str(input('您現在可以購買道具,是否購買?(True或False):')) if answer =='True': print('請說出要購買der道具') print('*'*41) print('*金幣翻倍器[現有的] 金幣翻倍器[得到的]*') print('* 2000G 1500G *') print('*'*41) answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):')) if gold >2000 and answer =='1': gold = gold - 2000 answer = int(input('請說出要翻der倍數:')) gold = str(gold * answer) print('您現在有'+gold+'金幣') gold = int(gold) elif gold >1500 and answer =='2': gold = gold - 1500 answer = int(input('請說出要翻der倍數:')) gold_mang = 100 * answer gold = gold + gold_mang print('您現在有'+str(gold)+'金幣') gold = int(gold) else: print('沒有這個道具,游戲結束') gold = 10000 elif answer =='False': print('繼續游戲') else: print('非法輸入') gold = 10000 if gold ==0: print('金幣沒了,游戲結束') gold = 10000 elif Game_start =='False': print('歡迎游玩,下次再見!') gold = 10000 else: print('非法輸入') gold = 10000 print('游戲結束,再見!') time.sleep(10)
以上就是使用python怎么制作一個猜數字游戲,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。