我正试图从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()
答案 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()