HS514 2022-02-23 12:55 采纳率: 68.8%
浏览 22
已结题

这段代码为啥要输出两次

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
prompt = "Please tell me something,and I will repeat it to you:\n"
prompt += "If you enter'quit',you will end the program.\n"
while True:
    if input(prompt) != 'quit':
        print(input(prompt))

    else:
        break

Please tell me something,and I will repeat it to you:
If you enter'quit',you will end the program.
a
Please tell me something,and I will repeat it to you:
If you enter'quit',you will end the program.
aa
aa
Please tell me something,and I will repeat it to you:
If you enter'quit',you will end the program.
s
Please tell me something,and I will repeat it to you:
If you enter'quit',you will end the program.
ss
ss
Please tell me something,and I will repeat it to you:
If you enter'quit',you will end the program.


  • 写回答

2条回答 默认 最新

  • 於黾 2022-02-23 13:07
    关注

    print(input(prompt))
    这是在玩啥呢
    你前面不把input的内容先保存起来,后面print
    而是用于判断是否是quit,后面用户必须再次输入一个内容,然后输出这个内容

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月23日
  • 已采纳回答 2月23日
  • 创建了问题 2月23日

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler