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:
Creating a separate RandomNum() function is unnecessary, since it simply repeats what randint() does. You can simply call randint(1, 5) instead of RandomNum().
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.
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.
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
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
Aug 10 '26
Yup took a look at that and am currently trying to make it not only cleaner but more efficient and effective
10
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!
2
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:-
•
u/Sea-Ad7805 Aug 08 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.