Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting this syntax error? (Python)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.")

>Solution :

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.") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading