-1

Hello programmers of StackOverFlow, I am a teenager that studies Python, And I can't find the logical errors in one of my codes. I wish you could help with that. I got two logical errors. 1. If the answer is "yes" or "no" it keeps opening the while commands while it shouldn't. 2. Concluding from first error it never stops when the choice == "yes" or "no"..

My coding is here:

choice = raw_input("Do you like programming? (yes/no) : ")
while choice != "yes" or "no":
    choice = raw_input('Come on, answer with a "yes" or with a "no" ! : ') 
if choice == "yes":
    print "I like you !"
elif choice == "no":
    print "I am sorry to hear that .. "

, Thank you in advance !

Josh E
  • 7,390
  • 2
  • 32
  • 44
Kostas Petrakis
  • 93
  • 3
  • 4
  • 8

2 Answers2

0

This is the problem:

while choice != "yes" or "no":

This is interpreted by Python as (choice != "yes") or ("no"). Since "no" is a string of non-zero length, it is truthy. This makes the or expression true because anything OR true is true. So your condition is always true and the loop never stops.

It should be:

while choice != "yes" and choice != "no":

Or:

while choice not in ("yes", "no"):
kindall
  • 178,883
  • 35
  • 278
  • 309
0

the second line evaluates to True. Because the string "no" evaluates to true

try the following

if "any string": print(1) #will always print 1, unless the string is empty

What you should use instead is

while choice not in ["yes", "no"]:

which checks if choice matches either "yes" or "no"

Mohammad Athar
  • 1,953
  • 1
  • 15
  • 31