Reputation: 21
Trying to use a while statement to return to an opening prompt. I apologize for my garbage code.
I've tried assigning a True/False value to the while loop but the program just terminates when any input is given.
choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))
while choice:
if choice == 1:
degreesF = eval(input('Enter the temperature in degrees F: '))
degreesC = 5/9*(degreesF - 32)
print(degreesC, 'degrees Celcius')
elif choice == 2:
distanceFeet = eval(input('Enter the distance in feet: '))
distanceMeters = distanceFeet/3.28
print(distanceMeters, 'm')
elif choice == 3:
Pounds = eval(input('Pounds: '))
Kilograms = Pounds*0.45359237038
print(Kilograms, 'kg')
elif choice == 4:
Ounces = eval(input('Ounces: '))
Liters = Ounces*0.0295735
print(Liters, 'L')
Currently, the program returns me to whatever I set the input as. For example, if I input 1, I can convert temperature but I am only able to convert temperature.
Upvotes: 2
Views: 1278
Reputation: 15300
You need to do a couple of things, and for now you should make them explicit:
Your program should run forever, or until it is told to quit.
Your prompt for a menu selection should loop until it gets a valid answer.
Your inputs for numeric values should loop until they get valid answers.
General Form of the Solution
How can you do these things? In general, by starting with bad initial data, and looping until you see good data:
some_value = bad_data()
while some_value is bad:
some_value = input("Enter some value: ")
A good "default" value for bad_data()
is the special value None
. You can write:
some_value = None
On the other hand, your is bad
test might want a different kind of bad data, such as a string. You might consider using ''
as your bad data value:
some_value = ''
Finally, if your is bad
test wants an integer value, maybe consider using a number you know is bad, or a number out of range (like a negative value):
some_value = 100
# or
some_value = -1
For your specific problems:
1. How do I run forever?
You can run forever using a while
loop that never exits. A while loop will run as long as the condition is true. Is there a value that is always true in Python? Yes! Its name is True
:
while True:
# runs forever
2. How do I loop until my menu selection is valid?
Using the general form, above, for an integer selection. Your menu asks the user to enter a number. You can check for a valid value either using str.isdigit()
or using try:/except:
. In python, the exception is the better choice:
choice = -1 # Known bad value
while choice not in {1, 2, 3, 4}: # {a,b,c} is a set
instr = input('Choose 1..4')
try:
choice = int(instr)
except:
choice = -1
3. How can I loop until I get a valid numeric value?
For floating point numbers, like temperatures, you don't want to try to spell out an explicit set of allowed answers, or a range of numbers. Instead, use None
(you could use Nan
, but it's more characters for the same result).
temp = None
while temp is None:
instr = input('Enter a temp: ')
try:
temp = float(instr)
except:
temp = None
Be aware that 'nan' is a valid float. So you might want to check for that and disallow it.
How do I combine all these things?
You put them together in blocks:
while True: # run forever
# Menu choice:
choice = -1 # Known bad value
while choice not in {1, 2, 3, 4}: # {a,b,c} is a set
instr = input('Choose 1..4')
try:
choice = int(instr)
except:
choice = -1
if choice == 1:
# Now read temps
temp = None
while temp is None:
instr = input('Enter a temp: ')
try:
temp = float(instr)
except:
temp = None
Upvotes: 1
Reputation: 9
You have to put the eval line inside your while loop for it to run multiple times.
untested (pseudo) code:
while true:
your input line
your other code
this will run forever so I suggest doing somethig like this
while true:
your input line
if input == 0:
break
your other code
this will stop the while loop when you type 0
Upvotes: 1
Reputation: 42748
You like to include the input in the while-loop:
while True:
choice = int(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))
if choice == 1:
degreesF = float(input('Enter the temperature in degrees F: '))
degreesC = 5/9*(degreesF - 32)
print(degreesC, 'degrees Celcius')
elif choice == 2:
distanceFeet = float(input('Enter the distance in feet: '))
distanceMeters = distanceFeet/3.28
print(distanceMeters, 'm')
elif choice == 3:
Pounds = float(input('Pounds: '))
Kilograms = Pounds*0.45359237038
print(Kilograms, 'kg')
elif choice == 4:
Ounces = float(input('Ounces: '))
Liters = Ounces*0.0295735
print(Liters, 'L')
else:
break
Upvotes: 3
Reputation: 7198
Putting below line inside a while True
may result in what you expected:
choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))
If I am not wrong, you need something like below:
while True:
choice = eval(input('What would you like to convert? \n Farenheit to Celcius (1) \n Feet to Meters (2) \n Pounds to Kilograms (3) \n Ounces to Liters (4) \n : '))
if choice == 1:
degreesF = eval(input('Enter the temperature in degrees F: '))
degreesC = 5/9*(degreesF - 32)
print(degreesC, 'degrees Celcius')
elif choice == 2:
distanceFeet = eval(input('Enter the distance in feet: '))
distanceMeters = distanceFeet/3.28
print(distanceMeters, 'm')
elif choice == 3:
Pounds = eval(input('Pounds: '))
Kilograms = Pounds*0.45359237038
print(Kilograms, 'kg')
elif choice == 4:
Ounces = eval(input('Ounces: '))
Liters = Ounces*0.0295735
print(Liters, 'L')
Upvotes: 3