我對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)
一次,然後重複調用其他功能?還是有更好的方法去做這件事?請記住,我是初學者,只是想學習,而不是開發人員。
非常感謝大家的回答!我終於找到了問題。我所要做的只是在'if'和'elif'語句的末尾添加'cmd = input(「請輸入新命令:」)',並在每個函數的末尾添加一個返回值。再次感謝您的幫助! – 2014-09-24 21:02:00