如何从用户输入调用该函数?

时间:2016-08-12 23:50:51

标签: python function python-3.x input

我正试图从Floyd_Warshall(G)致电user_command,但我得到error

Traceback (most recent call last):
File "main.py", line 24, in <module>
Floyd_Warshall()
NameError: name 'Floyd_Warshall' is not defined

如何调用它来摆脱这个错误?

import sys
import re

print("File containing graph: ")

while True:

   user_input = input()
   if user_input == "input1.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
      print("Enter command: ")
      user_command = input()
      if user_command == "help":
         print("Possible Commands are:")
         print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
         print("floyd - Runs Floyd's algorithm")
         print("help - prints this menu")
         print("exit or ctrl-D - Exits the program")
         elif user_command == "floyd":
         Floyd_Warshall()
      else:
         print ("dijkstra")
   elif user_input == "input2.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
   elif user_input == "input3.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
   elif user_input == "exit":
      sys.exit(0)
   else:
      print("Wrong choice")



def Floyd_Warshall(G):

............
............

if __name__ == '__main__':
    Floyd_Warshall()

2 个答案:

答案 0 :(得分:0)

应在调用之前定义

函数。所以在while循环之前放置函数

import sys
import re

# also this function needs one input mandatorily
def Floyd_Warshall(G):

............
............


print("File containing graph: ")

while True:

   user_input = input()
   if user_input == "input1.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
      print("Enter command: ")
      user_command = input()
      if user_command == "help":
         print("Possible Commands are:")
         print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
         print("floyd - Runs Floyd's algorithm")
         print("help - prints this menu")
         print("exit or ctrl-D - Exits the program")
         elif user_command == "floyd":
         Floyd_Warshall()
      else:
         print ("dijkstra")
   elif user_input == "input2.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
   elif user_input == "input3.txt":
      print("Possible Commands are:")
      print("dijkstra x - Runs Dijkstra starting at node X. X must be an integer")
      print("floyd - Runs Floyd's algorithm")
      print("help - prints this menu")
      print("exit or ctrl-D - Exits the program")
   elif user_input == "exit":
      sys.exit(0)
   else:
      print("Wrong choice")

if __name__ == '__main__':
    Floyd_Warshall()

答案 1 :(得分:0)

无论如何它都不会运行:

Floyd_Warshall() 

def Floyd_Warshall(G):

不兼容。该函数需要一个变量输入,当您使用Floyd_Warshall()

调用它时,它会丢失