首页 > 解决方案 > 如何在不使用 Python 列表的情况下存储用户输入?

问题描述

我目前正在尝试将用户输入存储为整数,而不将它们附加到列表或根本创建列表。

首先,我尝试为每个输入使用 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 还是很陌生。

标签: pythonstringinputintoutput

解决方案


我怀疑这是您真正被要求做的事情,但另一种方法是使用生成器表达式来避免完全存储变量。

user_inputs = (
   int(input(f'Please enter a {p} Fahrenheit temperature: '))
   for p in ('first', 'second', 'third', 'fourth', 'fifth')
)

print("The fahrenheits you entered are", *user_inputs)

推荐阅读