0
How to break this while loop
print("person one and two are allowed inside the club") person_one=18 person_two=27 while person_one >= 21: print("you may have a drink") else: print("you cannot have a drink") while person_two >= 21: print("you may have a drink") else: print("you cannot have a drink") if I put break at the end of it like this print("person one and two are allowed inside the club") person_one=18 person_two=27 while person_one >= 21: print("you may have a drink") else: print("you cannot have a drink") while person_two >= 21: print("you may have a drink") else: print("you cannot have a drink") break python says the break is outside of the loop. How do I fix this?
2 Respostas
+ 6
You seems to maybe confuse about 'while' loops and 'if' statements ^^
The 'break' is outside the loop, because the block of code repeating by the loop start on next line after the 'while", and end just previous indentation breaking ( at the 'else' line in your example code )... Are you sure you need loop at this place? Don't see the interest to infinitly loop on printing 'you may have a drink': think that you should use 'if" statement instead, which make a conditional testing only once, without repeating ( looping )...
+ 1
Try this:
print("person one and two are allowed inside the club")
person_one=18
person_two=27
if person_one >= 21:
print("you may have a drink")
else:
print("you cannot have a drink")
if person_two >= 21:
print("you may have a drink")
else:
print("you cannot have a drink")