+ 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?
5 Respostas
+ 9
You forgot to add colons.
else is also not correctly tabbed.
if input()==("nathan"):
print("trash")
else:
print("not trash")
+ 5
first get the input and assign it to variable:
x = input("input something")
Then test:
if "Nathan" in x:
do_stuff()
+ 2
>> don't forget to mark correct answers as correct (Swapnil) <<
+ 1
You have
if input ==("nathan")..
you should have
if input == "nathan":
+ 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