简体   繁体   中英

not printing if both statements are the same

I'm trying to print draw if players and computer choose the same variable, It can not printing if both statements are the same at the moment it just ends after the computers choice and ignores the if statement

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

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

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

#Write your code below this line 👇

players_choice = input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors. ")

if players_choice == "0":
  print(rock)
elif players_choice == "1":
   print(paper)
elif players_choice == "2":
  print(scissors)

import random

print("Computer chooses:")

computer_options = [rock, paper, scissors]

computer_choice = random.choice(computer_options)

print(computer_choice)

if computer_choice == players_choice:
  print("Draw")

Currently, you are comparing the players_choice which can be "0", "1" or "2" with computers choice that can be rock, paper or scissors,

That's why it can never be equal and can print Draw.

So simply you can set players_choice to something like below in your if, else blocks

if players_choice == "0":
    players_choice = rock
    print(rock)
elif players_choice == "1":
    players_choice = paper
    print(paper)
elif players_choice == "2":
    players_choice = scissors
    print(scissors)

So then this comparison will make more sense;

if computer_choice == players_choice:
  print("Draw")

Complete Updated Snippet:

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

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

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

players_choice = input("What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors. ")

if players_choice == "0":
    players_choice = rock
    print(rock)
elif players_choice == "1":
    players_choice = paper
    print(paper)
elif players_choice == "2":
    players_choice = scissors
    print(scissors)

import random

print("Computer chooses:")

computer_options = [rock, paper, scissors]

computer_choice = random.choice(computer_options)

print(computer_choice)

if computer_choice == players_choice:
  print("Draw")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM