Kabdmenrs
Kabdmenrs

Reputation: 45

My code works, in the sense that it runs what I want it to do, but once the user guess the number correctly, the terminal ends and does not loop

import random

number = random.randint(1,100)


while True:
    pick = input("Choose a number between 1 and 100. ")
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")

    while True:
        if pick > number:
            print("That was too high of a guess. Try again. ")
            break       
        elif pick < number:
            print("That was too low of a guess. Try again. ")
            break

the code runs the way i'd like it to, but it stops after one correct guess, and i'd like the loop to restart once the user guesses the number correctly

Upvotes: 1

Views: 74

Answers (3)

cdarke
cdarke

Reputation: 44394

You need to place the random number generation inside the loop as well, otherwise it will just test for the same number over and over:

import random

while True:
    number = random.randint(1,100)

    pick = input("Choose a number between 1 and 100. ")
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")

    while pick != number:
        if pick > number:
            print("That was too high of a guess. Try again. ")
            break
        elif pick < number:
            print("That was too low of a guess. Try again. ")
            break

Note that I replaced your while True infinite loop with a test for correctness.

Edit:

However, you can simplify it further:

import random

pick = 0
while True:
    number = random.randint(1,100)

    while pick != number:
        pick = input("Choose a number between 1 and 100. ")
        pick = int(pick)
        if pick == number:
            print("You are correct, great job! ")
            break
        elif pick > number:
            print("That was too high of a guess. Try again. ")
        else:
            print("That was too low of a guess. Try again. ")

Upvotes: 0

answerSeeker
answerSeeker

Reputation: 2772

Simply adding Continue fixes it. I used the number 5 to test the code.

import random

number = 5

while True:
    pick = input("Choose a number between 1 and 100. ")
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")
        continue

    while True:
        if pick > number:
            print("That was too high of a guess. Try again. ")
            break
        elif pick < number:
            print("That was too low of a guess. Try again. ")
            break

I further simplified your code below

import random

number = random.randint(1, 100)

while True:
    pick = input("Choose a number between 1 and 100. "+ str(number))
    pick = int(pick)
    if pick == number:
        print("You are correct, great job! ")
    if pick > number:
        print("That was too high of a guess. Try again. ")
    elif pick < number:
        print("That was too low of a guess. Try again. ")

Upvotes: -1

Djaouad
Djaouad

Reputation: 22776

Use one while and one break :

import random

number = random.randint(1,100)

while True:
    pick = int(input("Choose a number between 1 and 100. "))
    if pick == number:
        print("You are correct, great job! ")
        break # correct guess, break
    elif pick > number:
        print("That was too high of a guess. Try again. ")
    else: # one possibility left, so no need for another elif
        print("That was too low of a guess. Try again. ")

A test-case :

Choose a number between 1 and 100. 80
That was too high of a guess. Try again.
Choose a number between 1 and 100. 60
That was too high of a guess. Try again.
Choose a number between 1 and 100. 40
That was too low of a guess. Try again.
Choose a number between 1 and 100. 50
That was too low of a guess. Try again.
Choose a number between 1 and 100. 55
That was too high of a guess. Try again.
Choose a number between 1 and 100. 53
That was too low of a guess. Try again.
Choose a number between 1 and 100. 54
You are correct, great job!


If you don't want it to stop after a correct guess, remove the break, and guess again :

import random

number = random.randint(1,100)

while True:
    pick = int(input("Choose a number between 1 and 100. "))
    if pick == number:
        print("You are correct, great job! ")
        number = random.randint(1,100) # guess again
    elif pick > number:
        print("That was too high of a guess. Try again. ")
    else: # one possibility left, so no need for another elif
        print("That was too low of a guess. Try again. ")

Upvotes: 2

Related Questions