[英]How to call the function from user input?
我正在尝试从user_command
调用Floyd_Warshall(G)
,但是出现此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()
函数应在调用之前定义。 所以把函数放在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()
它无论如何都不会运行:
Floyd_Warshall()
和
def Floyd_Warshall(G):
不兼容。 该函数需要一个变量输入,当您使用Floyd_Warshall()
调用该函数时会丢失该输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.