【问题标题】:Python 2.7 for loop and not printing the right timePython 2.7 for 循环并且没有打印正确的时间
【发布时间】:2016-11-13 20:45:18
【问题描述】:

好的,所以我正在编写一个简单的登录系统,这是代码

import time
for counter in range(5):
  username=str(raw_input("Please enter your username"))
  password=str(raw_input("Please enter your password"))
  if username =="bros10":
     print "",username,"entered"
     time.sleep(4)
     print "You got the username right"
     if password =="brosbros10":
        print "You got the password right"
        print "Welcome to the system"
        break

else:
    print "Try again"

当我运行它时会发生什么,它会在我输入密码后打印这个位 print "",username,"entered" 任何帮助表示赞赏

【问题讨论】:

  • 我不确定我是否理解。您是否希望在密码之前打印它并在之后打印?如果是这样,则代码正在执行您所要求的操作。在输入password 之前尝试移动print
  • 你没有说出你的期望。你有什么问题?
  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已根据 CC BY-SA license 授予 SE 分发内容的不可撤销权利(即无论您未来的选择如何)。根据 SE 政策,分发非破坏版本。因此,任何破坏行为都将被撤销。请参阅:How does deleting work? …。如果允许删除,则帖子下方左侧有一个“删除”按钮,但仅在浏览器中,而不是移动应用程序中。

标签: python python-2.7 for-loop printing


【解决方案1】:

移动这个:

print "",username,"entered"

在此之前:

password=str(raw_input("Please enter your password"))

现在您的程序将要求输入用户名,重复输入的用户名(如果输入bros10,则告诉用户输入正确),然后要求输入密码,然后休眠 4 秒。

或者,您可以将 password= 行放在此行之前

if password =="brosbros10":

如果您希望它在等待 4 秒并重复用户输入后询问密码。

【讨论】:

    【解决方案2】:

    如果我理解你的正确你想在输入密码之前print "",username,"entered"。那么它应该是这样的:

    import time
    for counter in range(5):
      username=str(raw_input("Please enter your username"))
      print "",username,"entered"
      if username == "bros10":
         print "You got the username right"
         password=str(raw_input("Please enter your password"))
         time.sleep(4)
         if password =="brosbros10":
            print "You got the password right"
            print "Welcome to the system"
            break
    
      print "Try again"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2021-08-18
      相关资源
      最近更新 更多