Reputation: 5
This is my function for menu option 3, I think it isn't working because it'suppose to use info from option 2 to create new routes. But I would like to be able to still choose option 3 and just display a message that "no way points are available to create a route" if option 2 isn't chosen.
def menu3(waypoints, routes):
try:
num = int(input("How many routes would you like to create: "))
for i in range(0,num):
name = input("Enter route name: ")
routes[name] = waypoints
answer = ''
new_route = False
while answer != 'y' or answer != 'Y' or answer != 'n' or answer != 'N':
answer = input("\nWould you like to view your new route? (Y/N): ")
if answer == 'y' or answer == 'Y':
name = input("Enter route name: ")
for key in routes.keys():
if key == name:
print(routes[key])
new_route = True
if new_route == False:
print("The route has not been saved or created.")
break
elif answer == 'n' or answer == 'N':
break
Error()
except ValueError as e:
print("Error no Waypoints available to create new route")
and here is where I call it below
elif choice == 3:
menu3(waypoints, routes)
And my error is
File "C:/Users/Daniel/PycharmProjects/Assignment2/assignment2.4.py", line 102, in <module>
main()
File "C:/Users/Daniel/PycharmProjects/Assignment2/assignment2.4.py", line 94, in main
menu3(waypoints, routes)
UnboundLocalError: local variable 'waypoints' referenced before assignment`
Here's where settings are set:
def main():
choice = 0
location = []
routes = {}
while 1 == 1:
print("\nMENU")
print("********************************************************")
print("*1:\tCurrent location. (Current location saved as waypoint)")
print("*2:\tSelect to enter Waypoint(s)")
print("*3:\tCreate Paths using the waypoints you have saved")
print("*4:\tCalculate the distance to a waypoint from the current location")
print("*5:\tCalculate the direction as a compass bearing from the current location to a given waypoint.")
print("*6:\tExit program.")
print("*********************************************************")
try:
choice = int(input("Option: "))
except ValueError as error:
Error()
print(error)
if choice == 1:
location = menu1()
elif choice == 2:
waypoints = []
waypoints = menu2(waypoints)
elif choice == 3:
menu3(waypoints, routes)
elif choice == 6:
break
else:
print("Please enter a valid option.")
if __name__ == "__main__":
main()
Upvotes: 0
Views: 56
Reputation: 5866
"local variable 'waypoints' referenced before assignment" means waypoints doesn't exist yet. If you check your code, you're creating the variable when selecting the option 2:
elif choice == 2:
waypoints = [] # <---- here is the declaration.
Just add the declaration in before the while.
def main():
choice = 0
location = []
routes = {}
waypoints = [] # < --- Declaration
while 1 == 1:
print("\nMENU")
...
if __name__ == "__main__":
main()
Upvotes: 1
Reputation: 10090
You need to define waypoints
in your main method. Your main should look something like this - now when you call menu3(waypoints, routes)
before choosing option 2, waypoints will have been assigned a value:
def main():
choice = 0
location = []
routes = {}
waypoints = []
while 1 == 1:
print("\nMENU")
print("********************************************************")
print("*1:\tCurrent location. (Current location saved as waypoint)")
print("*2:\tSelect to enter Waypoint(s)")
print("*3:\tCreate Paths using the waypoints you have saved")
print("*4:\tCalculate the distance to a waypoint from the current location")
print("*5:\tCalculate the direction as a compass bearing from the current location to a given waypoint.")
print("*6:\tExit program.")
print("*********************************************************")
try:
choice = int(input("Option: "))
except ValueError as error:
Error()
print(error)
if choice == 1:
location = menu1()
elif choice == 2:
waypoints = [] # I left this because I assume you want to pass menu2 an empty list
waypoints = menu2(waypoints)
elif choice == 3:
menu3(waypoints, routes)
elif choice == 6:
break
else:
print("Please enter a valid option.")
if __name__ == "__main__":
main()
To check for this value in menu3()
, you can do something like this:
def menu3(waypoints, routes):
if waypoints:
num = int(input("How many routes would you like to create: "))
for i in range(0,num):
name = input("Enter route name: ")
routes[name] = waypoints
answer = ''
new_route = False
while answer != 'y' or answer != 'Y' or answer != 'n' or answer != 'N':
answer = input("\nWould you like to view your new route? (Y/N): ")
if answer == 'y' or answer == 'Y':
name = input("Enter route name: ")
for key in routes.keys():
if key == name:
print(routes[key])
new_route = True
if new_route == False:
print("The route has not been saved or created.")
break
elif answer == 'n' or answer == 'N':
break
Error()
else:
print("Error no Waypoints available to create new route")
Upvotes: 0