python-3.x - 如何在 Python 中获取多行输入(用于编程问题)?
问题描述
在编程问题中,我收到如下输入:
6
7
8
5
4
在这种情况下,我想创建一个类似 [6, 7, 8, 5, 4] 的列表,即输入中的数字列表。
我尝试直接从输入中读取,但后来我打印了我的列表,发现它是 [6],所以它只读取第一行。
lst = []
while True:
n = input()
if n != '':
lst.append(int(n))
else:
break
print(lst)
这给了我一个EOF错误。
解决方案
使用try-except
块
lst = []
while True:
try:
n = input()
lst.append(int(n))
except EOFError:
break
print(lst)
推荐阅读
- css - Mat-form-field 在不同的浏览器中具有不同的高度
- c# - c# Filter int for specific parameter
- swift - 登录应用程序时验证 inAppPurchase 订阅帐户还有哪些其他凭据
- node.js - Node.JS 服务层设计
- yocto - 根据 yocto 中的外部变量更改不同配方的内核配置
- python - 错误:AttributeError:“powerOfTwo”对象没有属性“x”
- scala - 线程会在这个 Promise 计算中停止吗
- azure-ad-b2c - Azure AD B2C:更新电子邮件以重置密码
- java - 是否可以创建由现有 HBase 表支持的 Phoenix 表?
- javascript - 是否存在 JavaScript 调用限制?