2014-09-22 53 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