我对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