-1
answer2 = input()
if answer == 'yes' or 'Yes' or 'ok' or 'sure':
    import random
    print Random([0,1,2,3,4,5])

I am trying to create a part of a program to generate a random response. Why is this showing up as an error? Or is there a better way to do this?

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • 3
    Please post the entire error message. Also include what `Random` is. Its not part of the stdlib. – tdelaney May 15 '18 at 22:02
  • 1
    Also, your `if` statement is always true. – kindall May 15 '18 at 22:03
  • Be careful of how boolean tests work. That should be `if answer == 'yes' or answer == 'no' or answer == 'ok' or answer == 'sure'` – tdelaney May 15 '18 at 22:03
  • 1
    Or as a slightly more compact method, `if answer in ('yes', 'no', 'ok', 'sure'):` – ShadowRanger May 15 '18 at 22:06
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – wwii May 15 '18 at 23:06

1 Answers1

0

If you want to generate a random number from a list of numbers try this:

answer = input()
if answer in ('yes', 'Yes', 'ok', 'sure'):
  import random
  print(random.choice([0, 1, 2, 3, 4, 5]))

Firstly, your if statement should be written like that. Secondly, if you want a random number from a range of numbers say 1-10, use random.randint(1, 10). But, if you have a list of numbers which are not necessarily in an order, use random.choice(list). Hope this helps!

IAmGroot
  • 81
  • 4