2

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.

2
  • "integer" should be "number" (or float, etc.). Commented Dec 6, 2012 at 5:18
  • 2
    For Python 2.x, I recommend you use float(raw_input()) instead of input(). Commented Dec 6, 2012 at 5:21

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

Will the break save, say 1 as the variable Rate?
@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.
@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.
0

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.

6 Comments

"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)
@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.
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?
@avasal Sorry about that, I'm new. is ther a way to pm you more questions?
@avasal What does the initial Rate = 0 do?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.