Python Forum
If statements won't work!!!! Plz help me. Thx - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: If statements won't work!!!! Plz help me. Thx (/thread-13562.html)



If statements won't work!!!! Plz help me. Thx - BubblesTheGiraffe - Oct-20-2018

Hi. I'm pretty new to python so this is probably a stupid question but simple IF statements don't work for me when I'm using variable = input for instance if I wrote something like

tuna = input("is tuna a fish")
if tuna=="yes":
         print("I know, course tuna is a fish")
if tuna=="no":
         print("What are you talking about, of course tuna is a fish")
It simply won't work. It doesn't even display an error no matter what I choose as to what tuna equals all that happens is it hits the end of the code with no words showing up. Does anybody know what I'm doing wrong? If so I would appreciate the help. Thx... Wall


RE: If statements won't work!!!! Plz help me. Thx - metulburr - Oct-20-2018

i am assuming you are not typing in yes or no? There is no condition for anything else. the second if should also be an elif. Finally You need an else clause for when the input is neither.

tuna = input("is tuna a fish")
if tuna == "yes":
    print("I know, course tuna is a fish")
elif tuna == "no":
    print("What are you talking about, of course tuna is a fish")
else:
    print('please enter yes or no')



RE: If statements won't work!!!! Plz help me. Thx - j.crater - Oct-20-2018

Hello and welcome to Python and the forums.
Next time use proper Python code tags, this time I added them for you.
What text do you enter when asked for input? Your if statements respond only to "yes" and "no" as an input. If you enter anything else (be it even "Yes", "YES", "No", "NO"...) the program will go over the checks and finish the program.
You should probably use if - elif - else to make your code more robust.


RE: If statements won't work!!!! Plz help me. Thx - Skaperen - Oct-21-2018

change your code so it types out what was typed in, while you are testing it. that may show something like an extra space. because the output does not have a space after "fish", maybe whoever is typing added a space while typing in.

print('so you typed in',repr(tuna))
the above is made for Python version 3.


RE: If statements won't work!!!! Plz help me. Thx - BubblesTheGiraffe - Oct-21-2018

Thank you, everyone, for the help. Of course, it was my silly error. I fixed it by putting yes directly next to the input question. Then I realized to put a space after fish. Ten hours of my day yesterday were spent learning the basics of python and this experience really helped Tongue


RE: If statements won't work!!!! Plz help me. Thx - Larz60+ - Oct-21-2018

A solution like metulburr's #2 covers all cases