如何使用用户输入创建类实例列表



我正在尝试创建一个程序,该程序将允许用户将数字(int not str(传递给列表,然后我可以将其传递给三个子类。我有父级温度,以及华氏,摄氏和开尔文的儿童课程。目标是让用户输入尽可能多的数字,直到他们输入"退出",然后将这些数字传递给子类,以便我可以根据这些数字来调用这些方法。我觉得我缺少一些非常简单的东西,但我不能放置它。我是编程的新手,所以请友善....

我已经尝试了以下代码,但是正在获得估值,我似乎找不到解决方案而不进一步破解问题。...

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 ()

所以,根据建议,我将其更改为:

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 ()

现在我得到了:

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'

当用户输入"退出"时,如何消除错误?

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 ()

相关内容

  • 没有找到相关文章

最新更新