【问题标题】:Print loop to the same line in terminal将循环打印到终端中的同一行
【发布时间】:2015-08-21 06:30:59
【问题描述】:

这是一些代码。它应该是不言自明的。但不管怎样,我们去吧。

   try:
        smtpserver.login(user, password)
        print "password is:  %s" % password
        break;
   except smtplib.SMTPAuthenticationError:
        print "wrong: %s" % password

除了打印行之外,所有的工作都有效

wrong: pass
wrong: pass
wrong: pass

在找到密码匹配之前是否可以在同一行打印?

【问题讨论】:

    标签: python


    【解决方案1】:

    在 python 中,打印语句末尾的, 可以防止换行,从而防止下一个文本在新行中打印。所以,

      try:
        smtpserver.login(user, password)
        print "password is:  %s" % password
        break;
      except smtplib.SMTPAuthenticationError:
        print "wrong: %s" % password,
    

    这将在同一行打印所有文本,直到密码正确

    【讨论】:

    • @MichaelDarnell:很高兴能提供帮助。如果对您有用,请随时接受我的回答。
    • 有没有办法只在原件上打印一次?
    • 我弄错了。我希望它只是错误一次,直到找到通行证然后打印找到,如果那是因为?
    • @MichaelDarnell:我认为这会很好:stackoverflow.com/questions/10829650/…
    • 我用“.”替换了“错误:%s”。这看起来很简单很好。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2022-11-02
    • 2017-09-26
    • 2013-11-30
    • 1970-01-01
    • 2021-10-12
    • 2013-06-27
    相关资源
    最近更新 更多