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

Python代码出现输出重复打印问题,编程新手寻求解决方案

Python代码重复输入问题的原因与解决方法

问题原因

  • 代码前两次调用input(Query_1)和input(Query_2)时,仅触发了用户输入操作,但没有将输入的内容保存到变量中,这两次输入的结果完全被丢弃了。
  • 最后一行print语句里再次调用input(Query_1)和input(Query_2),这会再次要求用户输入,所以才会出现需要输入两次的情况。

解决方法

把用户第一次输入的内容存储到变量中,后续直接使用变量进行字符串拼接即可,无需重复调用input函数。修改后的代码如下:

print("Welcome to Band Name Generator!")
Query_1 = "What is the name of the city you grew up in ? "
city = input(Query_1)
Query_2 = "What is your pet's name ? "
pet_name = input(Query_2)
print("Your band name could be " + city + " " + pet_name)

修改说明

  • 新增city和pet_name两个变量,分别存储用户输入的城市名和宠物名。
  • 最后一行print语句直接使用变量拼接字符串,不再触发新的输入操作,用户只需输入一次就能得到最终的乐队名称。

内容的提问来源于stack exchange,提问作者Xavier Eholade Young

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:00:04