def judge(player, AI):
if player == AI:
return -1
elif player != 0 and AI != 0:
return bool(abs(player) > abs(AI))
else:
return bool(player > AI)
import random as r
dict1 = {1: "石头", 0: "剪刀", -2: "布", "布":-2, "石头":1, "剪刀":0}
list1 = [1, 0, -2]
dict2 = {1: "你赢了", 0: "你输了", -1: "平局"}
while True:
str1 = input('请输入你想要出的,输入“退出”结束游戏')
if str1 == '退出':
break
if str1 not in dict1:
print('输入错误,请从(石头1、剪刀0、布-2)选择一个:')
else:
player = dict1.get(str1)
AI = r.choice(list1)
result = judge(player, AI)
print('你出的是:<%s>,电脑出的是 <%s>,%s'%(dict1[player], dict1[AI], dict2[result]))
|