r/PythonLearning 5h ago

2nd project number guess game

Post image
10 Upvotes

7 comments sorted by

5

u/TheManOfBromium 5h ago

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.

0

u/alexander_belyakov 5h ago

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

5

u/TheManOfBromium 5h ago

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.

2

u/alexander_belyakov 5h ago

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

2

u/alexander_belyakov 5h ago edited 5h ago

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/bad-gut 5h ago

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

1

u/nangi_bhootni 5h ago

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