My code is pretty much this:
Rate = input ("Enter Desired Rate of Charge: ") #User will be prompted to enter the charge rate of the system
if Rate < 0.5:
#If the charge rate entered is less than 0.5 kWhs
print "Charge Rate Too Low: Please consider revision" #Tells the user the rate is too low
elif Rate > 2.0:
#Also, if the charge rate entered is greater than 2.0 kWhs...
print "Charge Rate Too High: Please consider revision" #Tells the user the rate is too high
else:
#Otherwise, for any number entered between the bounds of 0.5 and 2.0 kWhs...
print '\n' #Page break for new conditions.
I need it to reprompt the user if the integer entered is less than 0.5 or greater than 2, and when the user does that, then save that integer as Rate and move on. Thank you.
BrenBarn
253k39 gold badges421 silver badges392 bronze badges
2 Answers 2
I think this is a bit cleaner than avasal's answer. This solution assumes that the user knows how to exit an application using CTRL+C though, otherwise you should add support for quitting the program when the user inputs Q or something.
rate = 0
while True:
rate = input("Enter desired rate of charge: ")
if not 0.5 < rate < 2:
print "Rate must be between 0.5 and 2."
else:
break
Brian Cain
14.6k3 gold badges53 silver badges89 bronze badges
answered Dec 6, 2012 at 5:24
Hubro
60.1k74 gold badges241 silver badges405 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
John Doe
Will the break save, say 1 as the variable Rate?
Hubro
@JohnDoe: No, the break will just break out of the loop. The loop will keep asking for a number until the user inputs a number between
0.5 and 2, then the loop will end and the input value will be in rate.glglgl
@JohnDoe It is already saved as
rate before the check. If it succeeds, it is just kept there. You can store it in Rate if you want, but that is bad style.Add a while loop till rate is less than 0.5 or rate is greater than 2.0
Rate = 0
while (Rate < 0.5) or (Rate > 2.0):
Rate = input ("Enter Desired Rate of Charge: ") #User will be prompted to enter the charge rate of the system
if Rate < 0.5:
#If the charge rate entered is less than 0.5 kWhs
print "Charge Rate Too Low: Please consider revision" #Tells the user the rate is too low
elif Rate > 2.0:
#Also, if the charge rate entered is greater than 2.0 kWhs...
print "Charge Rate Too High: Please consider revision" #Tells the user the rate is too high
else:
#Otherwise, for any number entered between the bounds of 0.5 and 2.0 kWhs...
print '\n' #Page break for new conditions.
answered Dec 6, 2012 at 5:18
avasal
14.9k4 gold badges33 silver badges49 bronze badges
6 Comments
Brian Cain
"reprompt the user if the integer entered is less than 0.5 or greater than 2" - change the condition to
Rate < 0.5 or Rate > 2. (or not (.5 < Rate < 2.), whichever's clearer)John Doe
@avasal It worked for < 0.5, but how do i get it to do the same for numbers integers than 2? Thank you for your answer btw.
John Doe
Problem Solved! Now I want to set a variable kWh equal to kWh = 80 - (a variable saved as Battery) - (a variable saved as remaining). However, it is telling me that kWh is invalid syntax. What should I change?
John Doe
@avasal Sorry about that, I'm new. is ther a way to pm you more questions?
John Doe
@avasal What does the initial Rate = 0 do?
|
lang-py
float, etc.).float(raw_input())instead ofinput().