r/pythonhelp • • Oct 22 '22

Anyway to call on a variable outside of a function?

So I have to make a game that involves 3 separate functions, one for calculations(), it just calculates random numbers. Another for user_guess(), displays the numbers and asks the user to guess. And lastly for the main game itself: main() , where it gives the user a score and repeatedly asks the user questions utilizing the other two functions.

So here’s my problem; within user_guess() is a user input variable. But because it’s localized to that function I can’t use it to verify if the user input is equal to calculations(). The user also needs to type ‘q’ to end the game. Does anyone know if there is any work-arounds to this?

0 Upvotes

2 comments sorted by

1

u/Goobyalus Oct 22 '22

You could use global variables. But generally you should avoid writing to global variables, and have your functions to take in the input that they need through arguments, and return the output that the callers need.

1

u/Zeroflops Oct 22 '22

Use the return keyword to return the variable from the function.