【问题标题】:How can I create a list of class instances using user input如何使用用户输入创建类实例列表
【发布时间】:2019-12-06 06:37:40
【问题描述】:

我正在尝试创建一个程序,该程序允许用户将一个数字(int 而不是 str)传递给一个列表,然后我可以将其传递给三个子类。我有父类 Temperature,子类 Fahrenheit、Celsius 和 Kelvin。目标是让用户输入任意数量的数字,直到他们输入“退出”,然后将这些数字传递给子类,以便我可以根据这些数字调用方法。我有一种感觉,我错过了一些非常简单的东西,但我无法放置它。我是编程新手,所以请善待......

我已经尝试了下面的代码,但是得到了一个 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'

当用户输入“退出”时如何消除错误?

【问题讨论】:

  • 您的代码有不止一个问题。请发送minimal reproducible example。至少,添加输入、输出和完整的错误消息。你可以edit这个问题。
  • 看来需要切换intinput这两个函数的顺序。 input 将返回一个字符串,然后您可以将其转换为整数。
  • 在循环之外创建您的列表,因为您在每次迭代中都执行temperatures = [],因此您的列表只能包含上次迭代的值。
  • 雅克,谢谢。我切换了 int 和输入,我还将“for temp in temperature”块放入循环中并运行,除了当我输入“quit”时,我得到另一个 ValueError: invalid literal for int() with base 10: 'quit '
  • input 是一个函数,所以你不能像你这样将它与字符串进行比较:input == 'quit'。我的意思是,你可以,但结果总是False,因为没有函数可以等于字符串。

标签: python python-3.x class user-interface user-input


【解决方案1】:
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 ()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 2020-08-17
    相关资源
    最近更新 更多