1 ''' 2 需求: 3 1. 通过人机交换实现您的出拳(input函数的应用) 4 2. 通过伪随机数模块random实现模拟对手出拳 5 3. 然后进行数据处理,得出结果 6 4. 输入数字非0、1、2退出 7 ''' 8 9 import random # 导入随机数模块random 10 11 while True: 12 # 人机交换: player表示玩家您出的拳 13 player = int(input("0-⽯头,1-剪⼑,2-布,请输入数字出拳:")) 14 if player not in [0, 1, 2]: 15 print("游戏已结束,欢迎下次光临!") 16 break 17 # 模拟对手出拳 18 computer = random.randint(0, 2) 19 20 if ((player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0)): 21 print(f"您赢了!player={player},computer={computer}") 22 elif (player == computer): 23 print("平局") 24 else: 25 print(f"您输了!player={player},computer={computer}")
说明:
搞懂需求,每次尽可能的完美。
标签:语句,random,player,computer,print,input,剪刀,出拳 From: https://www.cnblogs.com/allenxx/p/17527411.html