下面是規則.你和你的對手,在同一時間做出特定的手勢,必須是下面一種手勢:石頭,剪子,布.勝利者從
下面的規則中產生,這個規則本身是個悖論.
(a) 布包石頭.
(b)石頭砸剪子,
(c)剪子剪破布.在你的計算機版本中,用戶輸入她/他的選項,計算機找一個隨機選項,然后由你
的程序來決定一個勝利者或者平手.注意:最好的算法是盡量少的使用 if 語句
代碼如下:
#coding:utf-8
import random
guess_list = ["石頭","剪刀","布"]
guize = [["布","石頭"],["石頭","剪刀"],["剪刀","布"]]
while True:
computer = random.choice(guess_list)
people = input('請輸入:石頭,剪刀,布/n').strip()
if people not in guess_list:
people = input('重新請輸入:石頭,剪刀,布/n').strip()
continue
if computer == people:
print("平手,再玩一次!")
elif [computer,people] in guize :
print("電腦獲勝!")
else:
print("人獲勝!")
break
新聞熱點
疑難解答