Python程序循环

2024-09-30 20:37:15 发布

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

我正在尝试用python制作一种终端克隆(只是为了测试if else和elif stations的功能),我希望在用户向终端输入一些东西之后,不管输入完成了什么,它都会返回到可以输入一些东西的地方。假设用户输入

'帮助'

“帮助”菜单出现,然后终端输入“终端~~$”再次出现,供他们键入其他内容。你知道吗

或者如果用户输入

“瓦伊吉乌瓦威格”

终端状态:“无效输入” 我想让它回到终端输入。。(“终端~~$”) 我试过while循环,但似乎不起作用。希望这是有意义的。这是我到目前为止的代码(我只有tkinter,所以我可以做mainloop(),我不想为终端创建一个窗口(尽管那会很酷!)地址:

# Import wait settings and tkinter... we only need tkinter for the mainloop() setting, so the game doesn't close when it's finished #
import time
from tkinter import *

print("Terminal V.1.0 Alpha")
print("Type help for assistance with the terminal")

# Input a command into the terminal! #
terminalInput = input("Terminal~~$ ") 

# The inputs the terminal accepts #

if terminalInput == "help":
   time.sleep(1)
   print("HELP")
   print("Type: 'text = your_text' to make the terminal speak!")
   terminalInput = input("Terminal~~$ ")
   if terminalInput == "text = " + terminalInput:
      print("Terminal:\\" + terminalInput)

# Every input that the terminal does not accept #
else:
   print("Invalid Input: " + terminalInput)

master = Tk()

Tags: thetext用户import终端forinputif
3条回答

谢谢大家的帮助!这是我到目前为止得到的!请试一试!你知道吗

#Looking under the hood I see. Unless you know how to read Python, there are no secrets here ;)#

# Import wait settings #
import time

print("Terminal V.1.5 Alpha")
print("Type 'help' for assistance with the terminal.")

# Loop #
while True:
   # Input a command into the terminal! #
   terminalInput = input("Terminal~~$ ") 

   # The inputs the terminal accepts #

   # HELP #
   if terminalInput == "help":
      time.sleep(0.5)
      print(".")
      print("#~~Help~~#")
      print("Type: 'cred' for the credits to this program")
      print("Type: 'errorcodes' for common errors in the terminal")
      print(".")

   # CREDITS #
   if terminalInput == "cred":
      time.sleep(0.5)
      print(".")
      print("#~~Credits~~#")
      print("Elmar Peise/David Cullen: Help with looping the program!")
      print(".")

   # ADMIN CODE #
   if terminalInput == "adminpass123":
      time.sleep(0.5)
      print(".")
      print("Welcome Administrator")
      print(".")

   # ERROR CODES #
   if terminalInput == "errorcodes":
      time.sleep(0.5)
      print(".")
      print(".")
      print("1. ERROR CODE 404:")
      print("An invalid input has been entered to the terminal. (Such as 'blablabla' or 'jg48923qj09')")
      print("FIX:")
      print("Make sure your spelling is correct. This is a common mistake. Or try entering a valid input. 'help' can get you started on inputs")
      print(".")

   # ELSE #
   else:
      time.sleep(0.5)
      print(".")
      print("ERRORCODE 404: Invalid Input, Type 'errorcodes' for more info.")
      print(".")

一个简单的while循环可以帮助程序保持运行。你知道吗

代码如下:

import time

def help_loop():

    while True:
            try:
                print("Terminal V.1.0 Alpha")
                print("Type help for assistance with the terminal")

                # Input a command into the terminal! #
                terminalInput = input("Terminal~~$ ")

                print("Input: " + terminalInput) 

                if terminalInput.lower() == "help":
                    print("HELP")
                    print("Type: 'text = your_text' to make the terminal speak!")
                    try:
                        terminalInput = input("Terminal~~$ ")
                        if terminalInput == "text = " + terminalInput:
                           print("Terminal:\\" + terminalInput)

                    except Exception as ex:
                        print("Invalid Input: " + terminalInput)
            except Exception as ex:
                print("Invalid Input: " + terminalInput)

def main():

    help_loop()

if __name__ == '__main__':
    main()

试试这个:

while True:
    # Input a command into the terminal! #
    terminalInput = input("Terminal~~$ ") 

    # Indent rest of code so it's in the while block

相关问题 更多 >