Rock paper scissors skipping the result

import random
for i in range(3):
user = str(input("Please enter your choice: "))
if (random.randrange(3)) == 0 :
    print("Computer chooses Rock")
    if user == "scissors" :
        print("computer wins")
    elif user == "paper" :
        print("player wins")
    else :
        print("tie")
elif (random.randrange(3)) == 1 :
    print("Computer chooses Paper")
    if user == "rock" :
        print("computer wins")
    elif user == "scissors" :
        print("player wins")
    else :
        print("tie")
elif (random.randrange(3)) == 2 :
    print("Computer chooses Scissors")
    if user == "paper" :
        print("computer wins")
    elif user == "rock" :
        print("player wins")
    else :
        print("tie")

      

The formatting here is a little odd (this website was used before). I don't know the reason, but I don't know why this code sometimes misses the result. if anyone can help it would be great.

This is what gets created when it runs a couple of times

enter your choice: scissors
Computer chooses Rock
computer wins
enter your choice: scissors
Computer chooses Scissors
tie
enter your choice: scissors
Computer chooses Rock
computer wins
================================ RESTART ================================ 
Please enter your choice: scissors
Please enter your choice: rock
Computer chooses Rock
tie
Please enter your choice: rock
Computer chooses Rock
tie

      

I don't understand why it is missing the result. Seems to happen by accident

+3


source to share


5 answers


you don't have to use it random.randrange(3)

three times. This could be, for example, enter the following numbers: 1, 2 and then 0. So the code that is then executed would be:

if (1 == 0):
   ...
elif (2 == 1):
   ...
elif (0 == 2):
   ...

      

and none of the conditional blocks of the if statements will be executed.



Instead, do the following:

computerChoice = random.randrange(3)
...
if computerCoice == 0:
  ...
elif computerChoice == 1:
  ...
elif computerChoice == 2:
  ...
else
  raise Exception("something is definitively wrong here")

      

+5


source


you have used random.randrange(3)

several times, and each time there is a chance it is a different number. So I would suggest that you assign a value to a variable and then use it in your statements if

:



x = random.randrange(3)

      

+2


source


I see that you have accepted AndrΓ©'s excellent answer, which makes you clearly understand your mistake. Since I commented on your Q saying there are easier ways to award a hand, this is my trick.

import random

c = random.randrange(3)
u = int(input('1=scissors, 2=paper, 3=rock: '))-1

if u==c:
    print '... tie ...'
elif (u==0 and c==1) or (u==1 and c==2) or (u==2 and c==0):
    print 'User wins!'
else:
    print 'Computer wins... Booh!'

      

but I'm not sure if this is easier ... shorter but easier?

You can make it even shorter

import random

def hand():
    c = random.randrange(3)
    u = int(input('1=scissors, 2=paper, 3=rock: '))-1
    print "User played",['scissors,', 'paper,', 'rock,'][u],
    print "computer played",['scissors.', 'paper.', 'rock.'][c]
    print ['Tie.', 'User wins!', 'Computer wins...'][(c-u)%3]

      

This is an example session:

>>> hand()
1=scissors, 2=paper, 3=rock: 3
User played rock, computer played scissors.
User wins!
>>> 

      

+2


source


Yes, it happens randomly. :)

if (random.randrange(3)) == 0 :
  # computer "chooses" a random number
elif (random.randrange(3)) == 1 :
  # now the computer choice is a new choice from 0..2
elif (random.randrange(3)) == 2 :
  # and now it different again

      

+1


source


Your error is here:

if (random.randrange(3)) == 0 : elif (random.randrange(3)) == 1 : elif (random.randrange(3)) == 2 :

In the if statement, random.randrange (3) generates a random number, if it doesn't match, it goes into the elif statement, and random.randrange (3) will generate another random number and it may not be the same as the previously generated random number ... This will cause your code to miss one, two, three, or none of the statements for a while.

If you want your code to be good, you should assign a random number to an integer and then use that integer in your answer.

It should be

ran=random.randrange(3) if ran == 0 : elif ran == 2 : elif ran == 3 :

0


source







All Articles