0

I'm a beginner and have no idea what I'm doing wrong. Why the last statement "elif guess == number" doesn't work when I guessed the number? Here's my code:

import random

print("Welcome to the Number Guessing Game!\nI'm thinking of a number between 1 and 100.")
user_choice = input("Choose a difficulty. Type 'easy' or 'hard': ").lower()

attempts = 0
number = random.randint(1, 100)
print(number)
end_game = False

if user_choice == "easy":
    attempts = 10
elif user_choice == "hard":
    attempts = 5

while not end_game:
    print(f"You have {attempts} attempts remaining to guess the number.")
    guess = input("Make a guess: ")
    if guess != number:
        attempts -= 1
        if attempts == 0:
            print("You've run out of guesses, you lose.")
            end_game = True
    elif guess == number:
        print(f"You got it! The answer was {number}.")
        end_game = True
mgksmv
  • 5
  • 4
  • 2
    You're comparing a string to a number… – deceze Dec 07 '20 at 13:26
  • It's superfluous to repeat the negated condition. When `if guess != number` is *false*, then it's already proven that it false, then you don't need to do the opposite check again in an `elif`. Just `if ..: .. else: ..` is sufficient. – deceze Dec 07 '20 at 13:27

0 Answers0