繁体   English   中英

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

[英]How can I create a list of class instances using user input

我正在尝试创建一个程序,该程序将允许用户将数字(int而不是str)传递给列表,然后我可以将其传递给三个子类。 我的班级是体温班,孩子班级是华氏,摄氏和开尔文。 目标是让用户输入任意数量的数字,直到他们键入“ quit”,然后将这些数字传递给子类,以便我可以基于这些数字调用方法。 我觉得我缺少一些非常简单的东西,但是我无法放置它。 我是编程新手,所以请保持友好。

我已经尝试了下面的代码,但是得到了ValuError,如果不做进一步的修改,我似乎找不到解决方法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM