thyyyy1 2020-04-08 18:01 采纳率: 0%
浏览 439

目前学习python,在if判断这里有点问题,望大佬指点一下

问题: 1.从控制台输入要出的拳--石头(1)/剪刀(2)/布(3)
2.电脑随机出拳--先假定电脑只会出石头、完成整体代码功能
3.比较胜负
比赛规则: 石头胜剪刀 剪刀胜布 布胜石头

player = int(input("从控制台输入要出的拳--石头(1)/剪刀(2)/布(3):"))
computer = int(input("从控制台输入要出的拳--石头(1)/剪刀(2)/布(3):"))
if ((player == 1 and computer == 2)
        or (player == 2 and computer == 3)
        or (player == 3 and computer == 1)):
    print("玩家赢了")
elif player == computer:
    print("平局")
else:
    print("玩家输了")

这是我写的代码,可以正常运行,但是我现在考虑别的情况:
1.在游戏里过程中玩家如果出的不是1、2、3而电脑是123其中的一个,则判定为玩家不会玩。
2.在游戏里过程中电脑如果出的不是1、2、3而玩家是123其中的一个,则判定为电脑不会玩。
3.在游戏里过程中电脑和玩家出的都不是123,则判定电脑玩家都不会玩。
4.平局的情况,如果电脑玩家出的数字超过了123且是相同的,也判定为电脑玩家不会玩,而不是判定为平局。
以上4条问题,望大佬指定迷津
---------来自小白的迷茫

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-04-08 20:43
    关注
    player = int(input("从控制台输入要出的拳--石头(1)/剪刀(2)/布(3):"))
    computer = int(input("从控制台输入要出的拳--石头(1)/剪刀(2)/布(3):"))
    if player > 3 or player < 1:
      if computer > 3 or computer < 1:
        print("电脑玩家都不会玩")
      else:
        print("玩家不会玩")
    elif computer > 3 or computer < 1:
      print("电脑不会玩")
    else:
      if ((player == 1 and computer == 2)
              or (player == 2 and computer == 3)
              or (player == 3 and computer == 1)):
          print("玩家赢了")
      elif player == computer:
          print("平局")
      else:
          print("玩家输了")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?