May-09-2025, 10:23 AM
I am a total noob at learning python (day 6) and i gave myself a homework.
The program is a game where the user has to guess a number between 1-100. The computer then answers back if the secret number is "higher" or "lower" and the the user kepps guessing.
It is running so far, but i really can't wrap my head around why this statements at the end are not executed from that point on:
2nd question would be: What i wanted to do is to practise to encapsulate the core game into the function and then use the while loop only to execute the function and give back the number of guesses the user needed per round (the z=[]). But i struggled to do this. So if there are any tipps on this would appreciate it...
Thx and have a good one..
The program is a game where the user has to guess a number between 1-100. The computer then answers back if the secret number is "higher" or "lower" and the the user kepps guessing.
It is running so far, but i really can't wrap my head around why this statements at the end are not executed from that point on:
print(Game()) z.append(result) print(z)....Program:
z=[] keepPlay='y' while keepPlay=='y': def Game(): x=random.randint(1,100) y=0 i=0 while True: print('Please guess the number') y=int(input()) i+=1 if y>x: print('lower - your guess is to high') elif y<x: print('higher - your guess is to low') elif y==x: break if y==x: [b]print('well done you guessed my number in ' + str(i)+' tries[/b]') return i[/quote] result=Game() print(Game()) z.append(result) print(z) print('Want to play again y/n ?') a=input() if a=='y': keepPlay='y' elif a=='n': break #else: #print() print('Ok good bye then')The games runs till you pick the right number but then outputs:
Output:well done you guessed my number in 5 tries
then it immediatley jumps toOutput:Please guess the number
but it should have exited the while-loop after the break statement??2nd question would be: What i wanted to do is to practise to encapsulate the core game into the function and then use the while loop only to execute the function and give back the number of guesses the user needed per round (the z=[]). But i struggled to do this. So if there are any tipps on this would appreciate it...
Thx and have a good one..