0

I want to check what the user input was and if it was something else, it would print("") and continue.

Right now I have

userasnwer = int(input("reply with Hi or Hello or Yo"))

 if userasnwer == "Hi" or "Hello" or "Yo": 

  from random import randint 
  number = randint(1,3)

  if number == 1:
   print(awnser1)

  if number ==2:
   print(awnser2)

  if number == 3:
   print(awnser3)

But it justs prints one of them for any response. Can someone help me with how to respond only from the given?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Well this would throw an exception in a number of your cases because `int('Hi')` throws a `ValueError`. Use `if : ... elif : ... else: ...` – AChampion Jun 03 '18 at 17:07
  • 1
    if userasnwer in ["Hi", "Hello", "Yo"]: is what you are probably looking for – jbet Jun 03 '18 at 17:10
  • 1
    Can you post running code so we can see what you are doing? And 4 spaces per indent - its hard to spot logical blocks with 1 space.... its not like we are running out of spaces. – tdelaney Jun 03 '18 at 17:12

2 Answers2

0

So I think your issue stems from the line,

if userasnwer == "Hi" or "Hello" or "Yo": 

Here python checks if one of the three conditions is True.

First, it checks userasnwer == "Hi". Then, "Hello" or "Yo". If you open up a Python REPL or run a small program like,

if "hello":
    print("Hi!")

You'll see that "hello" evaluates to True. Do you see why your program runs incorrectly now?

Edit: I see your real question now but there was a bug hidden underneath that I helped squash for you. After your if-statement that checks for the useranswer you'll just need an else-statement. i.e..

if useranswer == 'Hi' or useranswer == 'Hello' or useranswer == 'Yo':
    # insert your logic here
else:
    print("No greeting!")

Note a more 'pythonic' way to write that first line would be,

if useranswer in ['Hi', 'Hello', 'Yo']:

This is a good link for Python Operators

Skam
  • 7,298
  • 4
  • 22
  • 31
0
userasnwer = input("reply with Hi or Hello or Yo")
if userasnwer == "Hi" or userasnwer == "Hello" or userasnwer == "Yo": 
    from random import randint 
    number = randint(1,3)

    if number == 1:
       print(awnser1)

    if number ==2:
       print(awnser2)

    if number == 3:
       print(awnser3)
ThePoetCoder
  • 182
  • 8