-1

My code bellow keeps outputting an error as variable "user_option" (which is holding an input value) is not available outside of the function. How do i make it available so i can use it for my if statement?

def main_menu(str):
    print(str(1) + " Play game")
    print(str(2) + " High score")
    print(str(3) + " Joke")
    user_option = input("What option would you like to select? ")
    return(user_option)

main_menu(str)

if user_option == "1" or user_option == "Play game" or user_option == "play game":

The reason I want it as a function is I have an option for the user to return to main menu screen and input a new option.

Megan59781
  • 31
  • 4

2 Answers2

3

You just need to collect the return value.

user_option = main_menu(str)

However, you may want to rename str as that is a built-in keyword in Python.

Erick Shepherd
  • 1,403
  • 10
  • 19
-4

If that is the only instance of that variable you can cast it as global

global user_option = input("What option would you like to select? ")
  • There is no need for global variables here. Check out [why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – DavidG Oct 31 '17 at 16:49
  • Not only is there no need for a global variable here, what is written is a syntax error. And "casting" to global doesn't make sense. – juanpa.arrivillaga Oct 31 '17 at 17:00