+ 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
8 Answers
+ 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?
+ 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
+ 2
Tomiwa Joseph you're most welcome. đ keep coding mate friend.
+ 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.
0
Thanks. It worked.
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
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.
0
Thanks anyway.