python - User only get three chances to enter a valid value -



python - User only get three chances to enter a valid value -

adding loop such user gets 3 chances come in valid value. if user enters invalid value more 3 times in row, programme should issue error message , exit

in python please know how while loop, how create 3 times

what have:

miles= eval(input("enter value miles ")) kilometers= miles * 1.6 while miles < 0: print("error, can't come in negative value miles") miles = eval(input("enter right value miles "))

i prepare code first...you're evaluating "miles < 0" after convert kilometers, whether or not user corrects miles variable, have resulting kilometers variable.

miles= eval(input("enter value miles ")) kilometers= miles * 1.6 while miles < 0: print("error, can't come in negative value miles") miles = eval(input("enter right value miles "))

change to:

miles = eval(input("enter value miles ")) while miles < 0: print("error, can't come in negative value miles") miles = eval(input("enter right value miles ")) kilometers= miles * 1.6 print(kilometers)

you can add together counter while loop take note of iterations, , stop when goes over:

counter = 0 miles = eval(input("enter value miles ")) while miles < 0: print("error, can't come in negative value miles") miles = eval(input("enter right value miles ")) counter+=1 if counter > 2: break kilometers= miles * 1.6 print(kilometers)

and create kilometer variable displays if counter doesn't exceed:

counter = 0 miles = eval(input("enter value miles ")) while miles < 0: print("error, can't come in negative value miles") miles = eval(input("enter right value miles ")) counter+=1 if counter > 2: break if counter <= 2: kilometers= miles * 1.6 print(kilometers) else: print("exceeded error count")

python loops

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -