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

Python指定次数输出语句代码未通过审核,求修正方案

问题分析与修正方案

代码问题点

  • 第二行输入错误将目标短语转为整数类型:b = int(input()),短语是字符串类型,用int()转换会导致非数字短语直接报错,应该直接读取字符串。
  • 打印语句的拼接方式过于繁琐,虽能输出格式,但可读性差、冗余。

修改后的代码

a = int(input())
b = input().strip()  # 读取字符串,strip()去除前后可能的多余空格
for _ in range(a):
    print(f'I like "{b}"!')  # 用f-string格式化,简洁直观

其他实现方式

  • 方式一:使用str.format()方法
count = int(input())
phrase = input().strip()
for _ in range(count):
    print('I like "{}"!'.format(phrase))
  • 方式二:利用字符串乘法一次性生成所有输出(减少循环内打印开销)
count = int(input())
phrase = input().strip()
output = f'I like "{phrase}"!\n' * count
print(output.strip())  # strip()去掉最后多余的换行

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:20:45