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

Python重复问候程序未触发是否继续询问问题如何修复

问题原因与修复方案

核心问题

  • 原有代码存在无用的死循环片段while play == "n": continue,逻辑完全无效,还会导致后续流程卡死。
  • 你写的“是否查看更多”逻辑没有和主循环打通,原有主循环从未更新play变量的值,每次问候输出完成后play还是初始的y,所以会直接进入下一轮循环、弹出姓名输入框,不会触发询问提示。
  • 不需要单独做play_2的嵌套循环,也不需要重复写姓名输入、问候输出的重复逻辑,复用现有主循环即可。

修复后完整代码(保留原英文提示)

print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ") 
while play == "y":
    name = input("Enter your name: ")
    print()
    greetings = int(input("How many times would you like to see your custom greeting?: "))
    print()
    for i in range(greetings):
        print("Hello " + name + "." + " It's a pleasure to meet you!")
    # 每次输出完成后询问是否继续,更新play值控制主循环
    play = input("Would you like to see more? (y/n): ")

print()                 
print("Bye. Thanks for using my program!")

中文交互版本(可选)

如果需要全中文操作界面,可以调整为以下代码:

print("本程序可按照你指定的次数重复输出自定义问候语")
print()
play = input("是否要继续使用?(y/n): ") 
while play == "y":
    name = input("请输入你的姓名: ")
    print()
    greetings = int(input("你想要输出多少次问候?: "))
    print()
    for i in range(greetings):
        print(f"你好{name},很高兴认识你!")
    play = input("是否要查看更多问候?(y/n): ")

print()                 
print("再见,感谢使用本程序!")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 12:24:03