Home > front end >  Why am I getting this syntax error? (Python)
Why am I getting this syntax error? (Python)

Time:12-22

Python noob here. Took a swing at the 'guess the number game' this afternoon. It all looks fine to me but i keep getting a syntax error on line 26:

else player_number >= secret_number:

I've tried everything but I can't figure it out at all. Thanks for your help.

import random, sys
secret_number = random.randint(1, 99)
countdown_timer = 7

print("This is a number guessing game.")
print("You have to guess a number between 1 and 99!")
print("You have 7 attempts to guess the correct number")
print("Good luck!")
print("\n")
print("Your first guess is: ")

while countdown_timer != 0:
    player_number = int(input())
    countdown_timer = (countdown_timer - 1)
    if player_number == secret_number:
        print("\n")
        print("That's it!! The number was: "   secret_number)
        print("\n")
        print("Congratulations!")
        print("Please try again.")
        quit()
    elif player_number <= secret_number:
        print("Higher!")
        print("You have "   int(countdown_timer)   "guesses left.")
        print("Please enter your next guess: ")
    else player_number >= secret_number:
        print("Lower!")
        print("You have "   int(countdown_timer)   "guesses left.")
        print("Please enter your next guess: ")
        
print("You are out of guesses, sorry.")
print("The correct number was: "   secret_number)
print("Please try again.")

CodePudding user response:

Change your else statement to elif. The else statement takes no expression. Therefore:

elif player_number >= secret_number:
    print("Lower!")
    print("You have "   int(countdown_timer)   "guesses left.")
    print("Please enter your next guess: ")

After actually running the code, I see you are trying to concatenate integer and string, but that won't work. To make it work, use the f' print.

Here is the code:

import random
secret_number = random.randint(1, 99)
countdown_timer = 7

print("This is a number guessing game.")
print("You have to guess a number between 1 and 99!")
print("You have 7 attempts to guess the correct number")
print("Good luck!")
print("\n")
print("Your first guess is: ")

while countdown_timer != 0:
    player_number = int(input())
    countdown_timer = (countdown_timer - 1)
    if player_number == secret_number:
        print("\n")
        print(f"That's it!! The number was: {secret_number}") # f' print here
        print("\n")
        print("Congratulations!")
        print("Please try again.")
        quit()
    elif player_number <= secret_number:
        print("Higher!")
        print(f"You have   {countdown_timer} guesses left.")  # here
        print("Please enter your next guess: ")
    elif player_number >= secret_number:
        print("Lower!")
        print(f"You have   {countdown_timer} guesses left.") #here
        print("Please enter your next guess: ")
        
print("You are out of guesses, sorry.")
print(f"The correct number was: {secret_number}") # and here
print("Please try again.") 

CodePudding user response:

I would replace the whole line with just "else:" and add a comment:

    if player_number == secret_number:
        ...
    elif player_number <= secret_number:
        ...
    else:
        # player_number >= secret_number
        ...
        
  • Related