【发布时间】:2019-09-21 02:03:00
【问题描述】:
我目前正在尝试将用户输入存储为整数,而不将它们附加到列表或根本不创建列表。
首先,我尝试为每个输入使用 5 个独立变量(下面的代码),当运行此代码时,它给出了以下内容:
您输入的华氏度为 (1, 2, 3, 4, 5)
我将如何删除这些括号?
firstFahr = int(input("Please enter a Fahrenheit temperature: "))
secondFahr = int(input("Please enter a Fahrenheit temperature: "))
thirdFahr = int(input("Please enter a third Fahrenheit temperature: "))
fourthFahr = int(input("PLease enter a fourth Fahrenheit temperature: "))
fifthFahr = int(input("Please enter a fifth Fahrenheit temperature: "))
enteredFahrs = firstFahr, secondFahr, thirdFahr, fourthFahr, fifthFahr
print("The fahrenheits you entered are", enteredFahrs)
感谢您提前提供的任何帮助,如果这似乎是一个菜鸟问题,我们深表歉意,因为我对 Python 还很陌生。
【问题讨论】:
-
为什么要避免列出清单?在这里,列表实际上是最明智的使用方法。 (请注意,在您粘贴的代码中,没有列表 - 只是一个元组。)
-
通常用于存储用户输入,使用常见的数据结构,如列表、元组、集合或字典。目前,您将输入存储为元组。我认为使用列表存储变量没有任何缺点。您可以创建这样的列表
enteredFahrs = [firstFahr, secondFahr, thirdFahr, fourthFahr, fifthFahr]
标签: python string input int output