2016-08-12 10 views
0

私は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() 

答えて

0

関数を呼び出す前に定義する必要があります。ループ

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() 
0

そのとにかく実行されないだろうがそうする前に機能を配置します。

Floyd_Warshall() 

def Floyd_Warshall(G): 

を互換性がありません。この関数には変数入力が必要ですが、答えを返す場合はFloyd_Warshall()

+0

となりますので、それを受け入れることを検討してください。 – Merlin

関連する問題