debugging 有人能帮我解决这个石头剪刀布游戏的结束语句,我正试图创建

xkrw2x1b  于 2023-03-30  发布在  其他
关注(0)|答案(3)|浏览(114)
import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''
anci = [rock,paper,scissors]

Opponent_choice = int(input("What do you choose? Type 0 for Rock, 1 for Paper, 2 for Scissors\n"))

print(anci[Opponent_choice])
# ------------------------------------
Comp_choice = random.randint(0,2)
Comp_choice_alt = anci[Comp_choice] 

print(f"Computer chose: {Comp_choice} \n {Comp_choice_alt}")

if Opponent_choice==0&Comp_choice == 0:
  print("It's a draw")
elif Opponent_choice == 0 & Comp_choice == 1:
  print("You lose")
elif Opponent_choice == 0&Comp_choice == 2:
  print("You win")
elif Opponent_choice == 1&Comp_choice == 0:
  print("You win")
elif Opponent_choice==1&Comp_choice == 1:
  print("It's a draw")
elif Opponent_choice == 1&Comp_choice == 2:
  print("You lose")
elif Opponent_choice == 2&Comp_choice == 0:
  print("You lose")
elif Opponent_choice == 2&Comp_choice == 1:
  print("You win")
elif Opponent_choice==2&Comp_choice == 2:
  print("It's a draw")

它不显示“你赢了”或“你输了”,有时不像它应该显示“这是一个平局”,它应该打印“你赢了”
我觉得我的if-else语句有问题

z31licg0

z31licg01#

将所有单个&更改为'and',请参阅代码。https://www.geeksforgeeks.org/difference-between-and-and-in-python/

if Opponent_choice ==  0 and Comp_choice == 0:
  print("It's a draw")
elif Opponent_choice == 0 and Comp_choice == 1:
  print("You lose")
elif Opponent_choice == 0 and Comp_choice == 2:
  print("You win")
elif Opponent_choice == 1 and Comp_choice == 0:
  print("You win")
elif Opponent_choice == 1 and Comp_choice == 1:
  print("It's a draw")
elif Opponent_choice == 1 and Comp_choice == 2:
  print("You lose")
elif Opponent_choice == 2 and Comp_choice == 0:
  print("You lose")
elif Opponent_choice == 2 and Comp_choice == 1:
  print("You win")
elif Opponent_choice == 2 and Comp_choice == 2:
  print("It's a draw")
vm0i2vca

vm0i2vca2#

应该使用and而不是&
https://www.geeksforgeeks.org/difference-between-and-and-in-python/

if Opponent_choice==0 and Comp_choice == 0:
    print("It's a draw")
  elif Opponent_choice == 0 and Comp_choice == 1:
    print("You lose")
  elif Opponent_choice == 0 and Comp_choice == 2:
    print("You win")
  elif Opponent_choice == 1 and Comp_choice == 0:
    print("You win")
  elif Opponent_choice==1 and Comp_choice == 1:
    print("It's a draw")
  elif Opponent_choice == 1 and Comp_choice == 2:
    print("You lose")
  elif Opponent_choice == 2 and Comp_choice == 0:
    print("You lose")
  elif Opponent_choice == 2 and Comp_choice == 1:
    print("You win")
  elif Opponent_choice==2 and Comp_choice == 2:
    print("It's a draw")
00jrzges

00jrzges3#

除了使用and之外,还可以简化条件逻辑。

if Opponent_choice == Comp_choice:
    print("It's a draw")
elif (
    (Opponent_choice == 0 and Comp_choice == 1)
    or (Opponent_choice == 1 and Comp_choice == 2)
    or (Opponent_choice == 2 and Comp_choice == 0)
):
    print('You lose')
else:
    print('You win')

相关问题