石头剪刀布python
石头剪刀布
import random
AI_result = ['石头','剪刀','布']
result = random.choice(AI_result)
User = input("请输入石头/剪刀/布:")
Win_list = [['石头','剪刀'],['剪刀','布'],['布','石头']]
print("AI出的",result,"玩家出的",User)
if result == User:
print("平局")
elif [User,result] in Win_list:
print("玩家获胜")
else:
print("玩家输了")
石头剪刀布(三局俩胜)
import random
AI_num = 0
User_num = 0
AI_result = ['石头', '剪刀', '布']
Win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
while AI_num < 2 and User_num < 2:
result = random.choice(AI_result)
User = input("请输入石头/剪刀/布:")
print("AI出的",result,"玩家出的",User)
if result == User:
print("平局")
elif [User,result] in Win_list:
print("玩家获胜")
User_num += 1
else:
print("玩家输了")
AI_num += 1
if User_num == 2:
print("最终玩家胜利了")
else:
print("最终AI胜利了")
或者使用break
import random
AI_num = 0
User_num = 0
AI_result = ['石头', '剪刀', '布']
Win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
while True:
result = random.choice(AI_result)
User = input("请输入石头/剪刀/布:")
print("AI出的",result,"玩家出的",User)
if result == User:
print("平局")
elif [User,result] in Win_list:
print("玩家获胜")
User_num += 1
else:
print("玩家输了")
AI_num += 1
if User_num == 2:
print("最终玩家胜利了")
break
elif AI_num == 2:
print("最终AI胜利了")
break