Home > Net >  Why does this snippet of code return one answer but sometimes two?
Why does this snippet of code return one answer but sometimes two?

Time:12-28

New Python programmer here. I'm trying to make a rock, paper, scissors game and I'm wondering why this code returns usually one answer but sometimes two. I'm trying to make it print one answer for choices1 and one for choices2.

Many thanks in advance :)

from random import randint
    


choices1 = ['rock', 'paper', 'scissors']
choices2 = ['rock', 'paper', 'scissors']

if randint(0,3) == 0:
    print(choices1[0])

elif randint(0,3) == 1:
    print(choices1[1])

elif randint(0,3) == 2:
    print(choices1[2])


pass


if randint(0,3) == 0:
    print(choices2[0])

elif randint(0,3) == 1:
    print(choices2[1])

elif randint(0,3) == 2:
    print(choices2[2])

CodePudding user response:

You're generating a new random number every time you call randint. What you want to do is generate a random number once and then do your if/elif checks.

result = randint(0,2)
if result == 0:
    print(choices1[0])
elif result == 1:
    print(choices1[1])
else:
    print(choices1[2])

In fact, you're repeating code here. A simpler solution would be

result = randint(0,2)
print(choices1[result])

As pointed out in the comments, you can even skip generating the index altogether and use the choice function provided by the random module:

result = random.choice(choices1)
print(result)

There are a few other issues. First, you're generating a number between 0 and 3, inclusively, but your arrays only have indices going up to 2. Second, you've got a pass statement in the middle of nowhere. pass does nothing and is only needed in situations where you're required to have at least one line of code. Third, there's no reason to have two separate lists, choices1 and choices2, which contain the exact same values.

Putting everything together,

choices = ['rock', 'paper', 'scissors']

result1 = random.choice(choices)
result2 = random.choice(choices)

print("Player 1's result is", result1)
print("Player 2's result is", result2)

CodePudding user response:

Since you are choosing one element from an array of elements, it would also make sense to define a function that randomly picked an element from an array. That way, you can do something like:

def choose_one(...)

first = choose_one(choices1)
second = choose_one(choices2)

It turns out, this is handy enough of a function to make it to the standard API, to:

https://docs.python.org/3/library/random.html#random.choice

So your code could be as simple as:

from random import choice

choices1 = ['rock', 'paper', 'scissors']
choices2 = ['rock', 'paper', 'scissors']

print(choose(choices1)
print(choose(choices2)

CodePudding user response:

you are calling randint() function again and again in the if else block. So, sometimes, it is giving two results and some times, it is giving single result. Also, you are calling randint(0,3), which will give values between 0 and 3. But, your if/else statment will not match when 3 is returned. you have to modify code as randint(0,2).

For getting two results always, you have call them initially and store the values. Use the stored value subsequently.

from random import randint
    


choices1 = ['rock', 'paper', 'scissors']
choices2 = ['rock', 'paper', 'scissors']

selectedChoice1 = randint(0,2)
selectedChoice2 = randint(0,2)


if selectedChoice1 == 0:
    print(choices1[0])

elif selectedChoice1 == 1:
    print(choices1[1])

elif selectedChoice1 == 2:
    print(choices1[2])


pass


if selectedChoice2 == 0:
    print(choices2[0])

elif selectedChoice2 == 1:
    print(choices2[1])

elif selectedChoice2 == 2:
    print(choices2[2])
  • Related