User only get three chances to enter a valid value
Question:
Adding a loop such that the user gets three chances to enter a
valid value. If the user enters an invalid value more than three times in a row, the program
should issue an error message and exit
In python please
I know how to do the while loop, but how do you make it to only three times
What I have:
miles= eval(input("Enter a value for miles "))
kilometers= miles * 1.6
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
Answers:
I’m not a python programmer but I can help you regarding to the logic behind it.
first declare a global variable for example integer. something like.
int invalid= 0;
Then do the loop using a do while statement or likewise loop.
while (true)
{
}
inside your while statement, put a validation on it or your logic and also the checking of number of iteration (not valid). something like
while (true)
{
bool valid = isthisValid(); // your method
if (valid == false)
{
invalid = invalid+ 1;
if (invalid >= 3)
{
// Throw your error here!
}
}
}
I would fix your code first…you’re evaluating “miles < 0” after you convert it to kilometers, so whether or not the user corrects the miles variable, you already have the resulting kilometers variable.
miles= eval(input("Enter a value for miles "))
kilometers= miles * 1.6
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
change to:
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
kilometers= miles * 1.6
print(kilometers)
You can add a counter to your while loop to take note of iterations, and stop when it goes over:
counter = 0
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
kilometers= miles * 1.6
print(kilometers)
And make it so that the kilometer variable only displays if the counter doesn’t exceed:
counter = 0
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
if counter <= 2:
kilometers= miles * 1.6
print(kilometers)
else:
print("Exceeded error count")
There are several ways to achive this. Here are two:
Using a for loop. And two check conditions:
import sys
def isvalid(input):
if input == 1:
return True
else:
return False
for n in xrange(3):
check = isvalid(input(">>"))
if check == True:
break
elif check == False and n == 2:
sys.exit("Exceed tries")
else:
print "Wrong input, try again"
print "Right"
Using a while loops and a counter: ( I prefere this )
import sys
def isvalid(input):
if input == 1:
return True
else:
return False
mistakes_counter = 0
while True:
check = isvalid(input(">>"))
if check == True:
break
else:
mistakes_counter += 1
if mistakes_counter == 3:
sys.exit("Exceed tries")
print "Wrong input, try again"
print "Right"
I understand your problem (because i already faced up a long time ago)
I find my solution myself… don’t give up just face it …i done this same….
Ohh sorry we are taking about your problem,not mine
…
Here a solution….`
for i in range(3):
pwd = float(input('enter password '))
if pwd == 123456:
#your work
#my recommended (function)
else :
i += 1
print("wrong password")
if i == 3:
print("wrong password limit exeded")`
I done for password u can edit that line to input
Adding a loop such that the user gets three chances to enter a
valid value. If the user enters an invalid value more than three times in a row, the program
should issue an error message and exit
In python please
I know how to do the while loop, but how do you make it to only three times
What I have:
miles= eval(input("Enter a value for miles "))
kilometers= miles * 1.6
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
I’m not a python programmer but I can help you regarding to the logic behind it.
first declare a global variable for example integer. something like.
int invalid= 0;
Then do the loop using a do while statement or likewise loop.
while (true)
{
}
inside your while statement, put a validation on it or your logic and also the checking of number of iteration (not valid). something like
while (true)
{
bool valid = isthisValid(); // your method
if (valid == false)
{
invalid = invalid+ 1;
if (invalid >= 3)
{
// Throw your error here!
}
}
}
I would fix your code first…you’re evaluating “miles < 0” after you convert it to kilometers, so whether or not the user corrects the miles variable, you already have the resulting kilometers variable.
miles= eval(input("Enter a value for miles "))
kilometers= miles * 1.6
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
change to:
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
kilometers= miles * 1.6
print(kilometers)
You can add a counter to your while loop to take note of iterations, and stop when it goes over:
counter = 0
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
kilometers= miles * 1.6
print(kilometers)
And make it so that the kilometer variable only displays if the counter doesn’t exceed:
counter = 0
miles = eval(input("Enter a value for miles "))
while miles < 0:
print("Error, you can't enter a negative value for miles")
miles = eval(input("Enter the correct value for miles "))
counter+=1
if counter > 2:
break
if counter <= 2:
kilometers= miles * 1.6
print(kilometers)
else:
print("Exceeded error count")
There are several ways to achive this. Here are two:
Using a for loop. And two check conditions:
import sys
def isvalid(input):
if input == 1:
return True
else:
return False
for n in xrange(3):
check = isvalid(input(">>"))
if check == True:
break
elif check == False and n == 2:
sys.exit("Exceed tries")
else:
print "Wrong input, try again"
print "Right"
Using a while loops and a counter: ( I prefere this )
import sys
def isvalid(input):
if input == 1:
return True
else:
return False
mistakes_counter = 0
while True:
check = isvalid(input(">>"))
if check == True:
break
else:
mistakes_counter += 1
if mistakes_counter == 3:
sys.exit("Exceed tries")
print "Wrong input, try again"
print "Right"
I understand your problem (because i already faced up a long time ago)
I find my solution myself… don’t give up just face it …i done this same….
Ohh sorry we are taking about your problem,not mine
…
Here a solution….`
for i in range(3):
pwd = float(input('enter password '))
if pwd == 123456:
#your work
#my recommended (function)
else :
i += 1
print("wrong password")
if i == 3:
print("wrong password limit exeded")`
I done for password u can edit that line to input