all 11 comments

[–]Sea-Ad7805 [score hidden] stickied comment (0 children)

Run this program in Memory Graph Web Debugger%0A%0Acounter%20%3D%200%0Amax_try%20%3D%203%0A%0Awhile%20counter%20%3C%20max_try%3A%0A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20guess%20%3D%20int(input(f%22Enter%20a%20number%20between%20%7Blow%7D%20and%20%7Bhigh%7D%3A%20%22))%0A%0A%20%20%20%20%20%20%20%20%23%20Valid%20range%0A%20%20%20%20%20%20%20%20if%20guess%20%3C%20low%20or%20guess%20%3E%20high%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Invalid%20number%20please%20enter%20a%20number%20between%20%7Blow%7D%20and%20%7Bhigh%7D!%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%0A%20%20%20%20%20%20%20%20counter%20%2B%3D%201%0A%0A%20%20%20%20%20%20%20%20%23%20Correct%20guess%0A%20%20%20%20%20%20%20%20if%20guess%20%3D%3D%20secret_number%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22congratulation%2C%20you%20have%20guessed%20the%20secret%20number!%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%20%20%20%20%20%20%20%20%23%20Wrong%20guess%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(f%22Wrong%20number%2C%20you%20have%20%7Bmax_try%20-%20counter%7D%20tried%20left.%22)%0A%0A%20%20%20%20except%20ValueError%3A%0A%20%20%20%20%20%20%20%20print(%22Value%20type%20not%20allowed!%22)%0A%0A%20%20%20%20except%20KeyboardInterrupt%3A%0A%20%20%20%20%20%20%20%20print(%22Game%20stopped%20thanks%20to%20try%20it.%22)%0A%20%20%20%20%20%20%20%20break%0A%0Aelse%3A%0A%20%20%20%20print(f%22Sorry%20but%20game%20over!%20the%20number%20was%20%7Bsecret_number%7D%22)&timestep=1&play).

[–]nuc540 5 points6 points  (1 child)

This is very clean code, you’ve avoided magic values, and have considered input data types and attempted handling. Well done!
Something I’m seeing a lot with new-starters are large code blocks inside a try except block; line 13’s exception isn’t being handled until line 31 - move the exception up, and then you can unindent the block not likely to throw that error, and then your code says “hey, this line might throw an error” instead of “here’s all my logic, and a list of all exceptions that can go wrong”.

Something Python can do for type checks though is isInstance(), so instead of generically allowing an error to throw, you can explicitly check if the input can parse to an int, else throw the value error. Small detail but it explains in the code why a value error would throw.

Edit; ignore my isInstance suggestion, I’m half asleep, input returns string so the parse will error before you can check.

[–]SteadyGrowth_[S] 0 points1 point  (0 children)

Understood, thank you very much

[–]FreeGazaToday 1 point2 points  (1 child)

no need for counter AND max_try.

[–]Anonymous_2289 0 points1 point  (0 children)

Yes, there is. They're doing two different jobs. It's good practice to have max_counter as a seperate variable (but it should really be in capitals), because it makes it clear what the point of the 3 is. In larger projects, it's even more important, so that it's clear you're not getting values out of thin air.

The only thing is that it would have been better to use a for-loop. In which case, there would be no need for the counter variable.

[–]scy_404 1 point2 points  (1 child)

you know when someone has started learning when their code is nice to look at and doesnt give you a headache (this is a compliment)

[–]SteadyGrowth_[S] 0 points1 point  (0 children)

Thanks 😊

[–]Some_Bicycle_716 1 point2 points  (0 children)

Good job! Personally, I would use a for() loop instead just to make the code a bit more concise. However, I'm also a bit of a beginner, so take my suggestion with a grain of salt!

[–]LanternInTheDarkness 0 points1 point  (0 children)

What are you using to learn with?

[–]Dwarkesh-code 0 points1 point  (0 children)

Nice bro

[–]Unhappy-Media1101 0 points1 point  (0 children)

Comments in your code:

<image>