Primo Primo - 4 years ago 106
Python Question

Python program gives wrong output when use break statement inside while loop

I am using break statement inside while loop to exit from while loop. But it gives wrong output. I don't know why this occurs. Here is the code I have used:

def func():
print "You have entered yes"
t='yes' or 'Y' or 'y' or 'Yes' or 'YES'
while True:
r=raw_input("Enter any number:")
if t=='r':
func()
else:
break
print "Program End"


Update:

When I put Yes it should give :

You have entered yes , but control goes to break statement. Why?

Answer Source

you should not use t = 'y' or 'Y' ... in your code because when u use or it checks the validity try this code and i am pretty sure it will work

 def func():
     print "You have entered yes"
 t=('yes', 'Y', 'y', 'Yes', 'YES')
 while True:
     r=raw_input("Enter any number:")
     if r in t:
         func()
     else:
         break
 print "Program End"   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download