應用:猜拳遊戲
<1>運行效果:
<2>參考代碼:
import random player = input('請輸入:剪刀(0) 石頭(1) 布(2):') player = int(player) # 產生隨機整數:0、一、2 中的某一個 computer = random.randint(0,2) # 用來進行測試 #print('player=%d,computer=%d',(player,computer)) if ((player == 0) and (computer == 2)) or ((player ==1) and (computer == 0)) or ((player == 2) and (computer == 1)): print('獲勝,哈哈,你太厲害了') elif player == computer: print('平局,要再也不來一局') else: print('輸了,不要走,洗洗手接着來,決戰到天亮')