如何创建循环的东西?

2024-05-29 08:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个有趣的项目,并创建了一个程序,当我第一次做它几个月前是无限的。我如何使它循环而不使它复制和粘贴自己一遍又一遍?你知道吗

这部分是创建一个登录序列

if Type == "Admin":
      Username = input("Username: ")
      if Username == "Admin":
          time.sleep(1)
          Password = input("Password: ")
          if Password == "Pass":
              Help = input("Correct. Logged in. What would you like to do now? Type help for options. ")
              if Help == "help":
                  print("Here's a list of things you can run.")
                  print("Paycheck, Status")
                  Command = input("Which command would you like to run? ")
                  if Command == "Paycheck":
                      print("You are currently being payed $0.")
                      time.sleep(5)
                  if Command == "Status":
                      print(
                          "2 members online currently. Servers open: Admin and User. Users online: "
                          "Admin, "
                          "User.")
                      time.sleep(5)
              if Help == "Status":
                  print(
                      "2 members online currently. Servers open: Admin and User. Users online: "
                      "Admin, "
                      "User.")
                  time.sleep(5)
              if Help == "Paycheck":
                  print("You are currently being payed $0.")
                  time.sleep(5)

在这里,我希望它再次询问他们想要输入什么命令,并且永远不要停止询问


Tags: youinputifadmintimestatususernamehelp
3条回答

在while循环中放入所有代码。比如:

while(1): #paste your code here

建议:

  • 首先让你的部分代码的功能
  • 然后在循环中调用该函数。你知道吗

此循环没有结束(或直到ctrl-c):

while True:
  another_answer = input("What can you do with the pieces of a broken heart?")

如果要循环直到满足条件,可以执行以下操作:

solved = False

while not solved:
    x = input("Try again")
    solved = x in ["some","acceptable","answers"]

使用while True可连续循环,使用while True中的break可中断循环(如果需要)。你知道吗

相关问题 更多 >

    热门问题