Home > OS >  Why doesn't the last line of this code work even when the conditions are met? This is from the
Why doesn't the last line of this code work even when the conditions are met? This is from the

Time:01-08

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")

for guesses_taken in range(1, 7):
    guess = int(input("Take a guess"))

    if guess < secret_number:
        print("This number is too low")
    elif guess > secret_number:
        print("This number is too high")
    else:
        break

    if guesses_taken == 6:
        print(f"Nope, the number I was thinking about was {secret_number}")
    
    #this line won't display even when the number is guessed correctly
    if guess == secret_number and guesses_taken < 6:
        print(f"Good job! You guessed my number in {guesses_taken} guesses!")

The original code had an indentation error, which I fixed by moving the last four lines of code to the loop teritory. Sadly, the last line still won't display even though the number was guessed correctly.

CodePudding user response:

That's because you have indentation wrong. Everything below break should be unindented, so that it's not part of the for loop:

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")

for guesses_taken in range(1, 7):
    guess = int(input("Take a guess"))

    if guess < secret_number:
        print("This number is too low")
    elif guess > secret_number:
        print("This number is too high")
    else:
        break

if guesses_taken == 6:
    print(f"Nope, the number I was thinking about was {secret_number}")
    
if guess == secret_number and guesses_taken < 6:
    print(f"Good job! You guessed my number in {guesses_taken} guesses!")

However, I would like to add that even this version of the code has a bug: if a user guessed correctly on his last attempt, the output would still be Nope, the number I was thinking about was {secret_number}

CodePudding user response:

try this code out to see if it solves your problem

`import random secret_number = random.randint(1, 20) print("I'm thinking of a number between 1 and 20")

for guesses_taken in range(1, 7): guess = int(input("Take a guess"))

if guess < secret_number:
    print("This number is too low")
elif guess > secret_number:
    print("This number is too high")
elif guess == secret_number:
    print(f"Good job! You guessed my number in {guesses_taken} guesses!")
else:
    break

if guesses_taken == 6:
    print(f"Nope, the number I was thinking about was {secret_number}")

#this line won't display even when the number is guessed correctly
if guess == secret_number and guesses_taken < 6:
    print(f"Good job! You guessed my number in {guesses_taken} guesses!")`

CodePudding user response:

First of all you don't really need the

if guesses_taken == 6:

The problem you're having is probably the break statement is on the same indentation level as the

if guess == secret_number and guesses_taken < 6:

Here is your code that should work fine.

import random
secret_number = random.randint(1, 20)
print("I'm thinking of a number between 1 and 20")

for guesses_taken in range(1, 7):
    guess = int(input("Take a guess"))
    if guess == secret_number:
        print(f"Good job! You guessed my number in {guesses_taken} guesses!")
        break
    elif guess < secret_number:
        print("This number is too low")
    else:
        print("This number is too high")
else:
    print(f"Nope, the number I was thinking about was {secret_number}")

You don't really need the < 6 check because this will happen when you exceed your for loops range which is from 1 till 7

CodePudding user response:

TL;DR: Remove lines 12-13

Lines 8 through 13:

if guess < secret_number:
    print("This number is too low")
elif guess > secret_number:
    print("This number is too high")
else:
    break

Think about this logically:

  • If the guess is less than the chosen number, inform the user that their guess is too low
  • If the guess is greater than the chosen number, inform the user that their guess is too higher
  • Otherwise, break out of the loop (essentially ending the code)

The only time when the 3rd condition would be fulfilled is if the number isn't too high, and isn't too low. The number is only both not too high and not too low if the number is equal to the chosen number. The else statement is what's causing your issues. Remove it.

  • Related