Python Forum
If statements won't work!!!! Plz help me. Thx
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statements won't work!!!! Plz help me. Thx
#1
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
Reply
#2
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')
Recommended Tutorials:
Reply
#3
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.
Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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
Reply
#6
A solution like metulburr's #2 covers all cases
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020