How could i emulate a repeat until loop in this code

python does not have a repeated until loop (i want python to keep on repeating until a condition is met)
number guessing - Replit

the code is here i want python to keep on asking for input and when i type anything invalid like saying d for the input it will stop the code is there a way for python just to say like “your input was not valid”?

name = input("Please enter your name: ")
print ("Welcome " + name + “.”)
print(“I am thinking of a number between 0-100. What is it?”)
import random

num = random.randint(0, 100)
guess = input(“Guess here:”)
guess = int(guess)

if guess==num:
print(“You are correct!”)
elif guess > num:
print(“Your guess is too big”)
else:
print(“Your guess is too small”)
##print(num)

You will want to use a while loop so your code will look something like this:

import random
name = input("Please enter your name: ")
print ("Welcome " + name + ".")
print("I am thinking of a number between 0-100. What is it?")
i = False
num = random.randint(0, 100)
while i == False:
    guess = int(input("Guess here:"))
    if guess == num:
        print("You are correct!")
        i = True
    elif guess > num:
        print("Your guess is too big")
    else:
        print("Your guess is too small")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.