0
Can somebody help me figure out why this is not working?
Can somebody figure out why this is not working. I am trying to enter an input and if it is the input in the if statment then I want it to print out a certain thing. I want to add more if statements if that is possible. I am fine if this way does not work and another way would work better. I tried puting else statments in there ,but that did not work. INPUT = str(input("Input:")) print(INPUT) if INPUT == (("Hi") or ("hi") or ("Hello") or ("hello")): print("\nHello") if INPUT == (("Goodbye") or ("goodbye") or ("Bye") or ("bye")): print("\nBye")
8 Answers
+ 1
INPUT = input().lower()
if INPUT in['hi', 'hello'] :
print("\nComputer:Hello")
elif INPUT in['goodbye', 'bye']:
print("\nComputer:Bye")
else:
print('Sorry can you repeat that please')
+ 3
You would need to do your comparisons like;
if INPUT == "Hi" or INPUT == "hi" ... etc:
but it may be easier to do something like;
if INPUT in ["Hi", "hi", "Hello", "hello"]:
or use the lower() method to change INPUT to all lower case so that you don't need to check for each case option.
Note you don't need to convert input() to str() as str is what is already returned from input.
INPUT = input("Input:").lower()
if INPUT in ['hi', 'hello']:
+ 3
Nate Bowles Can you show your code?
+ 2
Thanks for the help, and thanks for the extra information.
+ 2
also check the indentation after the colon (:) normally an indentation is four spaces from left to right.
+ 1
Yes,you can use multiple if in python multiple else if also by using syntax-
muliple if-
if(condition 1):
statement
if(condition 2):
stament
if(condition3):
statement
and so on
else if syntax-
if(condition 1):
statement
elif(condition 2):
stament
elif(condition3):
statement
and so on(and u can also use an else statement at the end, not compulsory)
The difference in execution of these syntaxes is that in multiple if even if first condition is satisfied it will check other conditions also while in else if if condition 1 is satisfied then it will not check other statement.
+ 1
I have another question, whenever I try to put in more than one input it has an error.(traceback string 1).
Can somebody give me an answer to why this happens.
0
Instead of putting the if statement within the previous if statement, I would add it after as an elif statement to check the variable's value a second time:
INPUT = lower(str(input("Input:")))
print(INPUT)
if INPUT == "hi" or INPUT == "hello":
print("\nHello")
elif INPUT == "goodbye" or INPUT == "bye":
print("\nBye")
Hope this helped!