我之前寫過一篇基於JS的石頭剪子布程序 《JS寫的一個石頭剪子布游戲,大家進來玩玩吧!》,今天又基於Python寫了一個實例,這裡邊的算法有點特殊但一時也想不到一個好的算法規律。
代碼:
# encoding=UTF-8
# 石頭剪子布 程序
# 李忠
import random
# 定義石頭剪子布字典
dict = {1:'剪子',2:'石頭',3:'布'}
for row in dict:
print '編號:',row,' = ',dict[row]
print '您出什麼?'
loop = True
while loop:
you = raw_input('請輸入編號回車: ')
try:
you = int(you)
if you>=1 and you<=3:
loop = False
else:
print '請輸入 1-3 范圍內的編號'
except Exception,e:
print '請輸入正確的數字編號'
dn = random.randint(1,3)
print '你出:',dict[you]
print '電腦出:',dict[dn]
print '結果:',
if dn==you:
print '平局'
elif (you>dn and you-dn==1) or you+2==dn:
print '你勝'
else:
print '電腦勝'
執行:
編號: 1 = 剪子 編號: 2 = 石頭 編號: 3 = 布 您出什麼? 請輸入編號回車: 3 你出: 布 電腦出: 石頭 結果: 你勝