Creating a random number guessing game.

While True will keep going until the loop is stopped with "break". The "continue" command restarts the current loop level. Your code could look something like this:

import random

from random import randint

def randompick():

num = randint(0, 10)

while True:

if int(input("Pick a number you think is the random number between 1 and 10: ")) == num:

print("You have guessed the number")

break

else:

continue

randompick()

You can also use Try / Except to validate the input and make sure it's a number, and not a letter:

import random

from random import randint

def randompick():

num = randint(0, 10)

while True:

try:

guess = int(input("Pick a number you think is the random number between 1 and 10: "))

except:

print('Not a number, try again.')

continue

if int(guess) == num:

print("You have guessed the number")

break

else:

continue

randompick()

/r/learnpython Thread