mardi 29 septembre 2020

How to end program with while loop in Python

Started learning python today and was working on a program that picks a random number and prompts the user to guess the number. I don't know how to get the program to stop running after the user guesses the number right the first time. Any help would be appreciated.

import random

def Guess():
    number = random.randint(0, 100)
    state = False
    tries = 0

    while state == False:
        print("Guess a number between 0 and 99")
        guess = int(input("What is your guess?: "))
        if guess == number:
            print("\nCorrect, the number was: " + str(number))
            print("\nThat took you " + str(tries) + " tries")
            return True
        elif guess > number:
            print("Wrong, Try a lower number\n")
            tries += 1
        elif guess < number:
            print("Wrong, Try a higher number\n")
            tries +=1 
Guess()



Aucun commentaire:

Enregistrer un commentaire