More Python Conditions Help


#1

I am using Python 3 something and my code isn't responding correctly. Whenever I type in No it says "To Bad"
When I type in something like maybe it says "I do not understand" but when I type Yes it also says "I do not understand but I want it to say "I forgot".

a = input("Wanna hear a joke? ")

if a != "No" and "Yes":
print("I do not understand")

else:
if a == "No":
print("To Bad!")

else:
  print("I forgot")

#2

a = input("Wanna hear a joke? ")

if a != "No" and "Yes":
  print("I do not understand")

else:
  if a == "No":
    print("To Bad!")
  elif a=="Yes":
    print("I forgot")

Added an elif in the else to check for Yes


#3

Thanks for the Help man :smiley:


#4

That doesn't work......... I am in python 3 and it isnt working for me why?


#5

Here's a slightly more intuitive way to write this code...

if(a=="No"):
     print("To Bad!")
elif(a=="Yes"):
     print("I forgot")
else:
     print("I do not understand")

This way, instead of checking first for if it's not "No" or "Yes", you can check if it IS "No" or "Yes" and if it ISN'T, then you print "I do not understand".

It's always better to make your code shorter, so it's easier to manage.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.