0

Here is my code, for some reason the outcome of the game is not displayed and I don't know why, can anyone explain how I can make the desired outcome be executed. I have tried messing around with my code for a while, if you know how to fix this can you teach me so that next time I won't make the same mistake. Thank you.

import random

Rock= 0
Paper = 1
Scissors = 2
Quest = input("Rock, Paper, Scissors?")
print("You choose",Quest)
AI = random.randint(0,2)

#What the A.I Chooses
if AI == 0:
    print("A.I Chooses Rock")

if AI == 1:
    print("A.I Chooses Paper")

if AI == 2:
    print("A.I Chooses Scissors")

# if you draw:
if AI == 0 and Quest == Rock:
    print("So, you draw")
if AI == 1 and Quest == Paper:
    print("So, you draw")
if AI == 2 and Quest == Scissors:
    print("So, you draw")
# Rock Possible Outcomes
if AI == 0 and Quest == Paper:
    print("So, you loose")
if AI == 0 and Quest == Scissors:
    print("So, you win")
# paper Possible Outcomes
if AI == 1 and Quest == Rock:
    print("So, you loose")
if AI == 1 and Quest == Scissors:
    print("So, you win")
#Scissors Possible Outcomes
if AI == 2 and Quest == Paper:
    print("So, you loose")
if AI == 2 and Quest == Rock:
    print("So, you win")
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Eren Ucar
  • 33
  • 6

2 Answers2

1

If you are entering the words relating to your choice, you should be aware that they will not be considered equal:

>>> if 0 == 'rock': print('match')
# no output

If you are entering the numbers relating to your choice, they will still be treated as a string (just a different string) and they will not be considered equal:

>>> if 0 == '0': print('match')
# no output

What you need to do is make sure you compare a number with a number:

>>> if 0 == int('0'): print('match')
match

You can do that by ensuring you enter the number-y string for your choice and turn it in to a number for comparison:

Quest = int(input("0 = rock, 1 = paper, 2 = scissors?"))

You could also just use the strings assuming that you compare against "rock" (the string) rather than rock the number. But, if you look at the code below, you'll see numbers can make your code a lot smaller.


If you want a more succinct implementation, you might want to consider this one:

import random
random.seed()
choices = ['rock', 'paper', 'scissors']

# Make sure user enters valid choice.

choice = ''
while choice not in choices:
    choice = input('Rock, paper, or scissors? ').lower()
print('You chose', choice)
you = choices.index(choice)

# AI just makes random choice.

ai = random.randint(0,2)
print('AI chose', choices[ai])

# Draw if both same, otherwise three scenarios in which player wins,
# otherwise AI wins.

if you == ai:
    print('A draw')
elif (you == 0 and ai == 2) or (you == 1 and ai == 0) or (you == 2 and ai == 1):
    print('You win')
else:
    print('AI wins')

It gets everything to a number as quickly as possible, to ensure comparisons are a simpler matter.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks for the advice, but why can I not simply use an AND gate in my code? – Eren Ucar Feb 12 '19 at 06:39
  • @ErenUcar, you *can,* assuming you're actually comparing the right things. As per my answer that is not currently the case, you compare a string against a number. – paxdiablo Feb 12 '19 at 07:23
0

As Rohit Raj mentions in a comment:

Rock, paper, scissors should be inside quotes in if statements as they are strings

placing them inside quotes solved the problem.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Eren Ucar
  • 33
  • 6