+ 2

I need help with this while loop

If the user gets it correct and plays again, the random integer(randint) remains the same. If I put the random integer inside the loop, it changes per guess. How can I make it change when the loop goes back to the beginning again assuming the randint is outside the loop? Or better still, inside the loop. https://code.sololearn.com/cvBbCB53ALpn/?ref=app

8th Apr 2020, 9:20 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
8 Respuestas
+ 3
import random x = random.randint(0, 100) print("Guess a number between 1 and 100") while True: num = int(input('Guess: ')) guess_count += 1 if num < x: print('Your number is too low.') if num > x: print('Your number is too high.') if num == x: print('Correct!') continu = input("Enter 'y' to play again or 'q' to quit.").casefold() if continu == 'y': x = random.randint(0, 100) continue if continu == 'q': break you can try this?
8th Apr 2020, 9:41 PM
RKK
RKK - avatar
+ 3
import random x = random.randint(0, 20) guess_limit = 5 guess_count = 0 print("Guess a number between 1 and 20") while guess_count < guess_limit: num = int(input('Guess: ')) guess_count += 1 if num < x: print('Your number is too low.') if num > x: print('Your number is too high.') if num == x: print('Correct!') continu = input("Enter 'y' to play again.").casefold() if continu == 'y': x = random.randint(0, 20) guess_count = 0 continue else: break
9th Apr 2020, 11:55 AM
RKK
RKK - avatar
+ 2
Tomiwa Joseph you're most welcome. 😁 keep coding mate friend.
9th Apr 2020, 5:31 PM
RKK
RKK - avatar
+ 1
I finally figured it out. Thanks very much for your help, RKK. It means a lot. https://code.sololearn.com/crE9c599rwxk/?ref=app Programming at times can be experimental. I kept thinking and trying.
9th Apr 2020, 5:30 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
Thanks. It worked.
9th Apr 2020, 11:13 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
One more issue though. So I improved the code a bit. The program quits when the user doesn't get the correct answer. Is there a way I can reduce the guess count back to 0 and play again? https://code.sololearn.com/cDhSzoIwrGk4/?ref=app
9th Apr 2020, 11:20 AM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
Thanks, but I want the program to ask if the player wants to play again even after not getting it right not only when they are correct.
9th Apr 2020, 4:53 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
0
Thanks anyway.
9th Apr 2020, 4:53 PM
Tomiwa Joseph
Tomiwa Joseph - avatar