You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何实现逐次询问用户输入10个整数并存储到列表中

Python实现输入10个整数存入列表的方案

你提供的原有代码缺少两个核心逻辑:一是没有提前初始化空的scores列表,二是循环判断条件仅校验了输入值的范围,没有限制输入次数为10次,以下是两种可用实现:


基础实现(固定输入10次,不校验输入范围)

# 初始化空存储列表
scores = []
# 循环执行10次输入
for _ in range(10):
    score = int(input("Please enter a bowling score between 0 and 300: "))
    scores.append(score)

print(scores)

带输入合法性校验的实现

保留原代码的0-300范围校验逻辑,仅合法值会被存入列表,非法输入会触发重输,直到凑满10个有效得分:

scores = []
# 列表长度不足10时持续循环
while len(scores) < 10:
    score = int(input("Please enter a bowling score between 0 and 300: "))
    if 0 <= score <= 300:
        scores.append(score)
    else:
        print("输入无效,请输入0到300之间的整数")

print(scores)

内容的提问来源于stack exchange,提问作者keira

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 15:45:01