r/PythonLearning • u/Necessary-Ad2110 • Jun 19 '26
Day 1 of Learning Python: "Bagels" Game
used: https://www.ray.so/ for image rendering (let me know if this is readable)
could be better, but no AI as it's fully handwritten
I'll be going over "The Big Book of Small Python Projects" by Al Sweigart
I am also learning DSA and going through Common Sense Guide to DSA
see you tomorrow!
87
Upvotes
2
u/denehoffman Jun 20 '26 edited Jun 20 '26
Nice work! If you’re open to some constructive criticism, read on:
A great next learning step would be format strings
An inline conditional would make it so you don’t have to write out the winning message twice when all you change is the plurality of the last word.
Try to avoid globals like has_won_game, random_num, and user_attempts and pass these into the functions instead. Global state is okay sometimes, but it can cause some really annoying bugs (for instance, if one of your functions took an argument called user_attempts, which value do you think takes precedence?).
You already know how to use break, so why not use a for loop over the allowed user attempts and just break when you win? Or better yet, wrap the game loop in a function and return whether or not the user won!
I don’t really understand your reason for letting the win state be None, it confuses the semantics of that variable. You also set it to False at the end of the user doesn’t win, but never read again after that, so there’s no point to setting it.
Add some validation on user input. If you don’t input a number, then you’ll get an error and a crash! Use a try-except instead, and then raise an exception on an invalid number range as well (and catch it). These sorts of validators also look nice when encapsulated into a function, imagine just writing user_num = get_input() and being sure that it’s a valid number which retries if the user puts garbage in!
+1 for ANSI codes, love that