+ 1

If input?

I'm trying to understand how to do an "if input ("texthere") then print a word. My code looks like this: If input == ("nathan") print ("trash") else print ("not trash") When I run it, it gives me the following error: File "..\Playground\", line 1 if input == ("nathan") ^ SyntaxError: invalid syntax Can someone please explain what is going on here?

8th Dec 2017, 9:27 PM
Ross Anderson
Ross Anderson - avatar
5 Respostas
+ 9
You forgot to add colons. else is also not correctly tabbed. if input()==("nathan"): print("trash") else: print("not trash")
9th Dec 2017, 2:08 AM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 5
first get the input and assign it to variable: x = input("input something") Then test: if "Nathan" in x: do_stuff()
8th Dec 2017, 9:48 PM
Eric Blinkidu
Eric Blinkidu - avatar
+ 2
>> don't forget to mark correct answers as correct (Swapnil) <<
29th Dec 2017, 5:26 AM
LunarCoffee
LunarCoffee - avatar
+ 1
You have if input ==("nathan").. you should have if input == "nathan":
8th Dec 2017, 10:06 PM
LordHill
LordHill - avatar
+ 1
if "nathan" in x: Will work as well, but it doesn't Check that it is Nathan, it just checks that it contains nathan. "nathan" and "HelloWorldnathanandstuff" would both return true, because both contain nathan. also, it helps to check x as either all capital letters or all lowercase letters to prevent case sensitive problems
9th Dec 2017, 12:51 AM
LordHill
LordHill - avatar