我正在尝试在python中进行一种终端克隆(只是为了测试if else和elif的功能)我想在用户输入一些内容到终端之后这样做,以及无论如何输入完成后,它会返回到可以输入内容的位置。所以让我们说用户输入
'帮助'
出现帮助菜单,然后再次显示终端输入“Terminal ~~ $”,以便他们输入其他内容。
或者如果用户键入
'waejhgiuoahweioug'
终端声明:'输入无效' 我希望它然后回到终端输入..(“终端~~ $”) 我尝试过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()
答案 0 :(得分:0)
试试这个:
while True:
# Input a command into the terminal! #
terminalInput = input("Terminal~~$ ")
# Indent rest of code so it's in the while block
答案 1 :(得分:0)
简单的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()
答案 2 :(得分:0)
谢谢大家的帮助!这是我迄今为止所拥有的!请试试吧!
#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(".")