Incorrect output printed while using random number selection

The code is correct (I think), but the output isn’t!

print ("You try to move the table.")
numbers = random.choice(list)
print ("You rolled a", numbers)
if numbers == "1" or "2" or "3" or "4" or "5" or "6" or "7":
   print ("Whoops! You failed to move the table. Look, it's heavy, okay? Don't think too bad of yourself.")
elif numbers == "8" or "9" or "10" or "11" or "12" or "13" or "14":
   print ("Ouch! You failed to move the table, and dropped it on your foot! That hurts...")
   health = 90
   print ("Your health is now", health)
elif numbers == "15" or "16" or "17" or "18" or "19" or "20":
   print ("You successfully move the table. It's heavy, but you manage.")

The first random number generated was “18”, but it printed the first line — “Whoops! You failed to move the table. Look, it’s heavy, okay? Don’t think too bad of yourself.” — instead of the correct line — “You successfully move the table. It’s heavy, but you manage.” Why isn’t it printing the correct line?

Please note that this isn’t all the code inputted, just the section that has problems.

The statement list is the name of a specific list in the code. (I googled how to use the random.choice function.) If I should change that, please let me know.

Yes, I have imported the “random” function.

Also, please let me know if I should show more information in order for this to make more sense.

(I’m writing a text-based adventure game! :slight_smile:)
View comments for updated code. (Please don’t question why I put it there… :slight_smile:)

That code doesn’t run at all.

Is it the format of the code? It runs in the program I use.
(I use codesandbox.io. )

It won’t run as posted because of the indentation.

This:

numbers == "1" or "2" or "3" or "4" or "5" or "6" or "7"

is treated as this:

(numbers == "1") or "2" or "3" or "4" or "5" or "6" or "7"

(numbers == "1") might be true or false, but "2", etc, are definitely true (treated as true), so the condition is always true.

1 Like

And the name random is not imported and list must be a list, not the builtin function. Please cut and paste exactly the code you run into questions, and minimize it.

2 Likes

@tjreedy Thanks for letting me know. I’ll edit the question to show all the code.
Thanks again!

Thanks! I fixed the indentation.
Do you mean that I should find another way to list the specific numbers?

Normally, a dice roll would be done with random.randrange rather than a choice from a list.

2 Likes

Yes. If you’re using strings, you could use a set (a tuple or a list would also work), but if you used numbers. you could check the range.

Here’s the most recent edited code (plus parts I should have added.) :

import time
import random
health = 100
roll = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
print ("You try to move the table.")
numbers = random.choice(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
print ("You rolled a(n)", numbers)
if numbers == "1" or "2" or "3" or "4" or "5" or "6" or "7":
    print ("Whoops! You failed to move the table. Look, it's heavy, okay? Don't think too bad of yourself.")
elif numbers == "8" or "9" or "10" or "11" or "12" or "13" or "14":
    print ("Ouch! You failed to move the table, and dropped it on your foot! That hurts...")
    health = 90
    print ("You lost ten health. Your health is now", health)
elif numbers == "15" or "16" or "17" or "18" or "19" or "20":
    print ("You successfully move the table. It's heavy, but you manage.")

This is using updated versions of the code plus parts I should have added.
I have tested all replies’ suggestions. Thank you everyone who helped!

roll = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

There’s a better way to make such a list: use range and list.

numbers = random.choice(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

You should be passing it a list, not each number separately. It’ll complain.

if numbers == "1" or "2" or "3" or "4" or "5" or "6" or "7":

These are strings, and the condition is always true, as mentioned in a previous post.

elif numbers == "8" or "9" or "10" or "11" or "12" or "13" or "14":

Same here.

elif numbers == "15" or "16" or "17" or "18" or "19" or "20":

Same here.

Hi - There are still a few issues with the code you posted:

  • variable roll is never used(you also do not need it)
  • random.choice does not take multiple arguments; random.choice(range(1, 21)) would work
  • instead of random.choice consider using random.randint: random.randint(1, 21)
  • consider renaming numbers to number since the returned value is a single returned value not multiple values; naming is really important to make code readable and maintainable
  • the conditional statements are still incorrect. Variable numbers is an integer not a string, so in principle they should not match; but since they are incorrect the first test will always succeed (as already pointed out by others).
  • it’s generally best to make a series of conditionals exhaustive, so to always end a series of if ... elif ... clauses with a simple else... Not doing so makes it easier for bugs to sneak into the code (since in some code paths none of the initial conditions might apply).
    You could improve the conditions by sth like (using number instead of numbers):
if number <= 7:
     print("... etc ...")
elif number <= 14:
     print("... etc...")
else:
     print("... etc...")

I hope this litany of issues doesn’t discourage you - It takes courage to put your code out there to be taken apart by other people, I hope you can use my comments to put it back together again :slight_smile:

Btw if you use roll, you could consider creating a special kind of fantasy “dice”, sth like:

roll = ('dragon', 'fairy', 'wiz', 'pythonista', 'bug')  
choice = random.choices(roll)   # choice now is a string
print("You rolled", choice)
match choice:
    case "dragon": print "You win!")
    case "bug": print("You lose!")
    case _: print("Once more?") 
1 Like

Thanks! This really helped. It works now! :partying_face: