python - 如何在不使用 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 还是很陌生。
解决方案
我怀疑这是您真正被要求做的事情,但另一种方法是使用生成器表达式来避免完全存储变量。
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)
推荐阅读
- apache-spark - 基于 spark 中列值的动态 regexp_extract
- canvas - 如何设置舞台背景图片
- java - java.lang.IllegalStateException:不是标准目录之一
- java - 将“2020-10-31T00:00:00Z”字符串日期转换为长
- perforce - Perforce P4Api.net add file get this Can't add filenames with wildcards [@#%*] in them
- java - 如果本地方法的包或类或方法名称被混淆,yGuard 现在会发出警告
- node.js - Firebase 堆栈“错误:实体的索引条目过多”
- android - 将凌空响应添加到全局变量
- excel - 如何在 excel 嵌套 AND 公式中显示哪些 AND 条件失败
- python - Neo4j 导入 csv 并在一列中拆分值