2014-09-22 31 views
0

我對Python非常陌生,對於一般的編程,所以我決定編寫一些基本代碼來幫助我瞭解它的來龍去脈。我決定嘗試做一個數據庫編輯器,並已制定了以下代碼:來自單個函數調用的連續結果

name = [] 
rank = [] 
age = [] 
cmd = input("Please enter a command: ") 

def recall(item): #Prints all of the information for an individual when given his/her name 
    if item in name: 
     index = name.index(item) #Finds the position of the given name 
     print(name[index] + ", " + rank[index] + ", " + age[index])  #prints the element of every list with the position of the name used as input 
else: 
    print("Invalid input. Please enter a valid input.") 

def operation(cmd): 
    while cmd != "end": 
     if cmd == "recall": 
      print(name) 
      item = input("Please enter an input: ") 
      recall(item) 
     elif cmd == "add": 
      new_name = input("Please enter a new name: ") 
      name.append(new_name) 
      new_rank = input("Please enter a new rank: ") 
      rank.append(new_rank) 
      new_age = input("Please input new age: ") 
      age.append(new_age) 
      recall(new_name) 
     else: 
      print("Please input a valid command.") 
    else: 
     input("Press enter to quit.") 

operation(cmd) 

我希望能夠調用operation(cmd),並從它可以調用盡可能多的功能/我想執行的許多行動。不幸的是,它只是無限打印其中一個結果,而不是讓我放入多個命令。

如何更改此功能,以便我可以撥打operation(cmd)一次,然後重複調用其他功能?還是有更好的方法去做這件事?請記住,我是初學者,只是想學習,而不是開發人員。

+0

非常感謝大家的回答!我終於找到了問題。我所要做的只是在'if'和'elif'語句的末尾添加'cmd = input(「請輸入新命令:」)',並在每個函數的末尾添加一個返回值。再次感謝您的幫助! – 2014-09-24 21:02:00

回答

0

看看你的代碼:如果你調用operation有什麼比「結束」,「召回」或「添加」

while cmd != "end": 
    if cmd == "recall": 

,內while病情True,未來if也是True,但後續if是錯誤的。因此,函數執行以下的塊

else: 
    print("Please input a valid command.") 

while循環繼續到它的下一圈。由於cmd沒有改變,所以相同的過程一遍又一遍地繼續。

+0

這很有道理......但我該如何解決這個問題?我如何更改我的代碼以執行我想要的操作? – 2014-09-22 22:51:09

+0

例如,你可以在'print('請輸入一個有效的命令。「)之後'返回'''' – 2014-09-23 00:55:16

+0

謝謝!現在它重複正確,但只在個別的'if'和'elif'語句中。我認爲問題在於該函數在函數的各個部分中仍爲'cmd'讀取相同的值。我會玩弄代碼,看看我是否得到任何結果。 – 2014-09-24 20:25:24

0

雖然您已經暗示operator_3來自命令行,但您還沒有在代碼中顯示operator_1,operator_2和operator_3來自哪裏。

您需要獲取一些代碼才能獲得「operator_3」的下一個值。這可能是從參數到function_3列表,在這種情況下,你會得到:

def function_3(operator_3): 
    for loopvariable in operator_3: 
     if loopvariable == some_value_1: 
#(and so forth, then:) 

function_3(["this","that","something","something else"]) 

或者,你可能會得到它的輸入(默認情況下,鍵盤):

def function_3(): 
    read_from_keyboard=raw_input("First command:") 
    while (read_from_keyboard != "end"): 
     if read_from_keyboard == some_value_1: 
#(and so forth, then at the end of your while loop, read the next line) 
     read_from_keyboard = raw_input("Next command:") 
+0

我不遵循,但那可能是我自己的愚蠢。我提出了我的代碼糟糕。對不起,麻煩... – 2014-09-22 05:28:44

0

問題您是否僅在function_3中檢查operator_3一次,第二次您向用戶詢問某個操作員時,您不存儲其值,這就是爲什麼它只能在一個條件下運行。

def function_3(operator_3): 
    while operator_3 != "end": 
     if operator_3 == some_value_1 
      function_1(operator_1) 
     elif operator_3 == some_value_2 
      function_2 
     else: 
      print("Enter valid operator.") # Here, the value of the input is lost 

你試圖實現的邏輯如下:

  1. 詢問用戶一些輸入。
  2. 用此輸入呼叫function_3
  3. 如果輸入不是end,請運行function_1function_2。再次
  4. 開始從步驟1

但是,你缺少#4上面,在那裏你試圖再次重新啓動循環。

要解決這個問題,請確保存儲用戶在爲操作員提示時輸入的值。要做到這一點,如果您使用的是Python3,請使用input函數,如果您使用的是Python2,請使用raw_input。這些功能提示某些輸入用戶,然後將輸入返回到您的程序:

def function_3(operator_3): 
    while operator_3 != 'end': 
     if operator_3 == some_value_1: 
      function_1(operator_3) 
     elif operator_3 == some_value_2: 
      function_2(operator_3) 
     else: 
      operator_3 = input('Enter valid operator: ') 
     operator_3 = input('Enter operator or "end" to quit: ') 
+0

我認爲你理解我所經歷的邏輯,但我不確定我是否遵守你的邏輯。我認爲我糟糕地提出了我的代碼。我將這個問題重寫得更加具體,所以希望這會有所幫助。 – 2014-09-22 05:20:52

0

看起來像你正試圖從用戶那裏得到輸入,但你從來沒有在function_3實現了它......

def function_3(from_user): 
    while (from_user != "end"): 
     from_user = raw_input("enter a command: ") 
     if from_user == some_value_1: 
     # etc... 
+0

我不確定我是否跟着你,但我認爲那是我的錯。我以高度抽象的方式呈現我的代碼,可能不正確。我用我正在使用的實際代碼更新了我的問題。 – 2014-09-22 05:25:33