I am trying to create a program that will allow a user to pass a number (int not str) to a list that I can then pass to three child classes. I have the parent class Temperature, and the child classes Fahrenheit, Celsius, and Kelvin. The goal is to have a user enter as many numbers as they like until they type 'quit' and then pass those numbers to the child classes so that I can call the methods based on those. I have a feeling that I am missing something very simple, but I can't place it. I am new to programming, so please be kind....
I have tried the code below, but am getting a ValuError and I can't seem to find a fix without breaking things further....
from Temperature import Temperature
from Fahrenheit import Fahrenheit
from Celsius import Celsius
from Kelvin import Kelvin
while True:
if input == 'quit':
break
else:
temperatures = []
print ("Fnter a value for each temp, or type 'quit' to finish")
f = Fahrenheit(input(int('Enter a temp in F: ')))
temperatures.append(f)
c = Celsius (input(int('Enter a temp in C: ')))
temperatures.append(c)
k = Kelvin (input(int('Enter a temp in K: ')))
temperatures.append(k)
for temp in temperatures:
print (temp.get_temp())
print (temp.above_freezing())
print (temp.convert_2F())
print (temp.convert_2C())
print (temp.convert_2K())
print ()
So, per suggestions, I have changed it to:
from Temperature import Temperature
from Fahrenheit import Fahrenheit
from Celsius import Celsius
from Kelvin import Kelvin
temperatures = []
while True:
if input == 'quit':
break
else:
print ("Enter a value for each temp, or type 'quit' to finish")
f = Fahrenheit(int(input('Enter a temp in F: ')))
temperatures.append(f)
c = Celsius (int(input('Enter a temp in C: ')))
temperatures.append(c)
k = Kelvin (int(input('Enter a temp in K: ')))
temperatures.append(k)
for temp in temperatures:
print (temp.get_temp())
print (temp.above_freezing())
print (temp.convert_2F())
print (temp.convert_2C())
print (temp.convert_2K())
print ()
And now I get this:
Enter a value for each temp, or type 'quit' to finish
Enter a temp in F: 5
Enter a temp in C: 5
Enter a temp in K: 5
The current temperature is: 5
(False, '5 degrees Fahrenheit is at or below freezing')
The current temperature is 5 degrees Fahrenheit.
5 degrees Fahrenheit is -15.0 degrees in Celsius.
5 degrees Fahrenheit is 258.15 Kelvin.
The current temperature is: 5
(True, '5 degrees Celsius is above freezing')
5 degrees Celsius is 41.0 degrees in Fahrenheit.
The current temperature is 5 degrees Celsius.
5 degrees Celsius is 278.15 Kelvin.
The current temperature is: 5
(False, '5 Kelvin is at or below freezing')
5 Kelvin is -450.66999999999996 degrees in Fahrenheit.
5 Kelvin is -268.15 degrees in Celsius.
The current temperature is 5 Kelvin.
Enter a value for each temp, or type 'quit' to finish
Enter a temp in F: quit
Traceback (most recent call last):
...line 14, in <module>
f = Fahrenheit(int(input('Enter a temp in F: ')))
ValueError: invalid literal for int() with base 10: 'quit'
How do I eliminate the error when the user inputs 'quit'?
from Temperature import Temperature
from Fahrenheit import Fahrenheit
from Celsius import Celsius
from Kelvin import Kelvin
prompt = "\nEnter a number for each temp, and I will give you all the info you need."
prompt += "\n(Don't worry, we'll store all your info as we go along so you don't lose anything)"
prompt += "\nYou can hit 'Enter' to add numbers or type 'quit' to stop the loop: "
temperatures = []
while True:
message = input(prompt)
if message == 'quit':
break
else:
print ("Enter a value for each temp, or enter 'quit' to finish")
f = Fahrenheit(int(input('Enter a temp in F: ')))
temperatures.append(f)
c = Celsius (int(input('Enter a temp in C: ')))
temperatures.append(c)
k = Kelvin (int(input('Enter a temp in K: ')))
temperatures.append(k)
for temp in temperatures:
print (temp.get_temp())
print (temp.above_freezing())
print (temp.convert_2F())
print (temp.convert_2C())
print (temp.convert_2K())
print ()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.