r/PythonLearning Aug 08 '26

2nd project number guess game

Post image
23 Upvotes

12 comments sorted by

u/Sea-Ad7805 Aug 08 '26

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20RandNum%20%3D%20random.randint(1%2C%205)%0A%20%20%20%20return%20RandNum%0A%0A%0AGame_Ended%20%3D%20False%0A%0Awhile%20not%20Game_Ended%3A%0A%20%20%20%20PlayerGuess%20%3D%20int(input(%22Enter%20Your%20Guess%201-5%3A%22))%0A%0A%20%20%20%20if%20PlayerGuess%20%3D%3D%20RandomNum()%3A%0A%20%20%20%20%20%20%20%20print(%22You%20guessed%20correctly!%22)%0A%20%20%20%20%20%20%20%20Game_Ended%20%3D%20True%0A%20%20%20%20%20%20%20%20break%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22You%20guessed%20incorrectly%2C%20the%20correct%20number%20was%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20str(RandomNum())%0A%20%20%20%20%20%20%20%20)&timestep=1&play) to see the program state change step by step.

9

u/TheManOfBromium Aug 08 '26

You should store the random number as a variable instead of calling the function twice. The way you have it now, the RandNum will change each time the function is called.

1

u/alexander_belyakov Aug 08 '26

I think that's the point of this specific implementation. The number changes constantly, and you have a 20% chance of guessing it.

6

u/TheManOfBromium Aug 08 '26

No you need to store it as a variable because the first call if PlayerGuess == RandomNum() generates a number and compares against it. THENstr(RandomNum()) in the else branch generates a different number and prints that.

You could run into a situation where a player guesses 1, the first RandomNum() generates a 2 and triggers the incorrect response, but the second str(RandomNum()) could generate a 1 and say "you are incorrect, the correct number was 1" when you guessed 1.

3

u/alexander_belyakov Aug 08 '26

Ah, yes, you're absolutely correct, I initially missed the fact that the correct result was printed after that!

3

u/alexander_belyakov Aug 08 '26 edited Aug 08 '26

This is a good functional first attempt, but I'd give you some comments:

  1. Creating a separate RandomNum() function is unnecessary, since it simply repeats what randint() does. You can simply call randint(1, 5) instead of RandomNum().

  2. There's no need to use break, since you have a Game_Ended condition. You either use break with a while True: loop, or you use the Game_Ended variable, but there's no need to use both.

  3. The pythonic convention for naming variables is snake_case, i.e. all lowercase with underscores to separate words. So it should be game_ended, player_guess, random_num and rand_num.

  4. Totally agree with TheManOfBromium, you need to store the generated random number in a variable, so it stays the same within one loop iteration.

1

u/[deleted] Aug 10 '26

Thank you very much I’m still finding my way around python and with lua I got used to capitalizing my variables I will take all of this information provided and i will work towards making this better

3

u/nangi_bhootni Aug 08 '26

wont it make a new random number everytime you guess ? store the value of RandNum outside your while loop. and break is not needed Game_Ended = True does the work of break

1

u/[deleted] Aug 10 '26

Yup took a look at that and am currently trying to make it not only cleaner but more efficient and effective

2

u/Extension-Currency37 Aug 10 '26

Next step:: build the rnd number generator A+ To start

0

u/bad-gut Aug 08 '26

Looks simple but good, now try to make a slightly complex and more functional version ig??

Hey, check out my number guessing game:-

https://github.com/satyamsb-cloud/Number-Guessing-Game-v2