python 實現人和電腦猜拳的示例代碼
完成人機猜拳互動游戲的開發,用戶通過控制臺輸入實現出拳,電腦通過程序中的隨機數實現出拳,每一局結束后都要輸出結果。當用戶輸入n時停止游戲,并輸出總結果。
import randomall = [’石頭’,’剪刀’,’布’]computer = random.choice([’石頭’,’剪刀’,’布’])#所有贏了的情況win = [[’石頭’,’剪刀’],[’布’,’石頭’],[’剪刀’,’布’]]class Text(): def func_play(self): ind = input(’請輸入【0】石頭【1】剪刀【2】布’) if ind.isalpha(): try: raise ValueError(’請輸入數字’) except ValueError as v: print(v) elif ind.isdigit(): ind = int(ind) if 0<=ind<=2: play = all[ind] print(’你輸入的是%s,電腦輸入的是%s’%(play,computer)) if play == computer: self.a = ’平局’ elif [play, computer] in win: self.a = ’你贏了’ else: self.a = ’你輸了’ else: print(’請輸入0到2之間的數’) print(self.a) def write_file(self): with open(’wuhan.txt’,’a’,encoding=’utf-8’) as f: f.write(self.a+’n’)while True: t = Text() t.func_play() t.write_file()
到此這篇關于python 實現人和電腦猜拳的示例代碼的文章就介紹到這了,更多相關python 人和電腦猜拳內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: