r/pythonhelp • u/K1ngkai • Mar 07 '22
Question about restarting loop and tracking scores on number game
So i have been working on a number game where the program picks a number and the user has to guess the number. the one problem I am having is trying to start the loop from the start. the second part I dont know where to start is how to track the users lowest guess being the best "attempt." If you guys could please help me in any way, that would be awesome lol.
1
u/Goobyalus Mar 08 '22
You've got this global attempt declared in main, but the variable isn't defined in the top level (where it would be global), and game defines a local attempt variable.
And I'm not sure what attempts() in main supposed to do.
I think you meant to have attempt = <something> at the top level, and global attempt inside game so game uses the global attempt count.
The way your score message is written, game is meant to have the user play repeatedly, but it calls another_game. Instead, put the internals of the game into a loop so that if the user wants to continue, it repeats. Then you don't need another_game, or a global variable.
2
u/K1ngkai Mar 08 '22 edited Mar 08 '22
first of all thank so much for taking the time to help me.
for the global attempt I was trying to make it so later I can track the lowest attempt. I guess my next question is, how do I re-call the past attempts? through a list so then I can find the lowest one?
I was also trying to see if I could track the lowest attempt by naming it prev_score but still not sure how to track it.
1
u/Goobyalus Mar 08 '22
You can do it without global, for example:
def main(): # initialize variables that are relevant to the whole game session while True: # Play one game and get the number of guesses guess_count = game() # Update game session data like best score so far and give user info # Check if the user wants to play more user_input = input("Would you like to keep playing?") if user_input.lower() not in ["y", "yes"]: break # Do cleanup stuff for the end of the game
gamecan return the relevant info and let the caller decide how to deal with that data, which is usually what you want to do.If you do want
gameto update data, you would just have the global variables initialized at the top level of the script, andglobal xxxxinsidegamefor the variables you wantgameto be able to access.
1
u/Goobyalus Mar 08 '22
^ These don't do what you think they do. They're like doing
The expressions
'No'and'Yes'are non-emptry strings which are "truthy", so these if statements always fall in.