2017-04-20 97 views
-3

我試圖在python中做一種終端克隆(只是爲了測試如果其他的和elif的聲明的力量)我想這樣做後,用戶輸入的東西進入終端,而且無論輸入的內容是否完整,都會返回到您可以輸入內容的位置。因此,可以說在用戶輸入Python程序循環

「幫助」

顯示的幫助菜單,然後終端輸入「終端~~ $」再次出現爲他們提供了輸入別的東西。

,或者如果用戶類型

「waejhgiuoahweioug」

終端狀態:「輸入無效」 我希望它再回到終端輸入。(「終端~~ $」) 我已經試過while循環,但是你似乎沒有工作。希望這是有道理的。這是到目前爲止我的代碼(我只有Tkinter的,所以我可以做的主循環()我沒有試圖讓該終端窗口(雖然這將是酷)!):

# 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

那麼,究竟是什麼問題圍繞此循環?你到目前爲止的嘗試是什麼?你需要什麼'tkinter'? –

+0

使用while循環做到這一點: –

+0

使用while循環不會這樣的:笑 無效輸入: 終端V.1.0 APLHA 用於與終端 終端~~ $笑 無效的輸入幫助 鍵入help笑 無效的輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無效輸入:笑 無限.... –

回答

0

試試這個:

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

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

感謝你們倆。當我將其添加到我的github中時,您將會獲得學分! –

+0

它的工作原理!非常感謝! –

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() 
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(".") 
+0

stackoverflow不適用於討論。請不要在答案部分發表評論。 –