簡體   English   中英

單個函數調用的連續結果

[英]Continuous results from a single function call

我對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) ,然后重復調用其他函數? 還是有更好的方法來做到這一點? 請記住,我是一個初學者,只是嘗試學習,而不是開發人員。

盡管您已經暗示了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:")

問題是您僅在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,您試圖重新啟動循環。

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

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: ')

看起來您正在嘗試從用戶那里獲取輸入,但是您從未在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...

看一下您的代碼:

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

如果使用“ end”,“ recall”或“ add”之外的任何值調用operation ,則while內的條件為True ,下一個if也是True ,但隨后的if s為false。 因此,該函數執行以下塊

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

while循環繼續進行到下一圈。 由於cmd尚未更改,因此相同的過程一遍又一遍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM