猜拳
#循环猜拳 # 0:剪刀,1:石头 ,2:布 # c i # 0 0 平局 # 0 1 赢 # 0 2 输 # 1 0 输 # 1 1 平局 # 1 2 赢 # 2 0 赢 # 2 1 输 # 2 2 平 import random dict = {0: '剪刀', 1: '石头', 2: '布'} while True: a = random.randint(0, 2) i = int(input("请出拳,用数字代替你想出的拳:0:'剪刀',1:'石头',2:'布'")) if i < 0 or i > 2: print('请输入1至2的数字') print('您出的拳为: ', dict[i]) print('您的对手出的拳为', dict[a]) if i == a: print('平局', dict[i]) elif a == 0: if i == 1: print('您赢了') else: print('您输了') elif a == 1: if i == 0: print('您输了') else: print('您赢了') elif a == 2: if i == 0: print('您赢了') else: print('您输了')
#可以退出的
import random dict = {0: '剪刀', 1: '石头', 2: '布'} while True: while True: i = input("请出拳,用数字代替你想出的拳:0:'剪刀',1:'石头',2:'布',q:退出游戏") if i == 'q': exit('游戏结束') if i.isdigit(): i = int(i) else: print('请输入0至2的数字') break a = random.randint(0, 2) if i < 0 or i > 2: print('请输入0至2的数字') break print('您出的拳为: ', dict[i]) print('您的对手出的拳为', dict[a]) if a-i == 0: print('平') elif a-i == -1 or a-i == 2: print('win') else: print('lose')
#循环猜拳且输入值错误会返回 import random dict = {0: '剪刀', 1: '石头', 2: '布'} print("请出拳,用数字代替你想出的拳:0:'剪刀',1:'石头',2:'布'") while True: while True: i = int(input()) a = random.randint(0, 2) if i < 0 or i > 2: print('请输入0至2的数字') break print('您出的拳为: ', dict[i]) print('您的对手出的拳为', dict[a]) if a-i == 0: print('平') elif a-i == -1 or a-i == 2: print('win') else: print('lose')
#输入不规范也会返回 import random dict = {0: '剪刀', 1: '石头', 2: '布'} while True: while True: i = input("请出拳,用数字代替你想出的拳:0:'剪刀',1:'石头',2:'布'") if i.isdigit(): i = int(i) else: print('请输入0至2的数字') break a = random.randint(0, 2) if i < 0 or i > 2: print('请输入0至2的数字') break print('您出的拳为: ', dict[i]) print('您的对手出的拳为', dict[a]) if a-i == 0: print('平') elif a-i == -1 or a-i == 2: print('win') else: print('lose')