Когда вход вводится в недопустимый цикл while, он перезапускает функцию. Однако цикл не прерывается после любого ввода после перезапуска функций.
вот программа:
def main():
type_of_user = input("Are you a student? ")
while True:
if type_of_user.lower() == "y":
print("Your price is $3.50/m")
break
elif type_of_user.lower() == "n":
print("Your price is $5.00/m")
break
else:
print("Not valid")
main()
Если вы вводите y в первый раз, когда она работает, и цикл прерывается.
Are you a student? y
Your price is $3.50/m
, если вы вводите n в первый разработает, и цикл прерывается:
Are you a student? n
Your price is $5.00/m
, если вы вводите неверный ввод в первый раз, цикл никогда не прерывается, даже если ввод y или n:
Are you a student? b
Not valid
#now loop continues
Are you a student? y
Your price is $3.50/m
Not valid #also prints not valid after valid inputs
Are you a student? n
Your price is $5.00/m
Not valid
Are you a student?