Syntax error in Python code. What is it?

Asked

Viewed 4,541 times

1

print("**************")
print("Seja Bem Vindo")
print("**************")
numero_secreto = 65
chute = input("Digite um numero:")
print("Você digitou: ",chute)
if numero_secreto == chute
    print("você acertou")
else
    print("Você errou, Tente novamente")

I don’t understand why it doesn’t rotate

  • 1

    Have you read the error messages? They have written what is wrong.

  • I read but don’t understand

3 answers

2

Error of syntax missed placing two stitches on if and in the else

print("**************")
print("Seja Bem Vindo")
print("**************")
numero_secreto = 65
chute = input("Digite um numero:")
print("Você digitou: ",chute)
if chute == numero_secreto:
    print("você acertou")
else:
    print("Você errou, Tente novamente")

See example Online

Example of if with else

if expression:
   statement(s)
else:
   statement(s)

but, comparison also contains problems, or places numero_secreto = "65" your comparison will work because the input returns a text value, or else put the int (int(input("Digite um numero:")) and let the numero_secreto = 65 also as whole so that it can compare and have no problems in comparison.

Source: Python IF... ELIF... ELSE Statements

  • 1

    Ata, thank you man

1


There are errors of syntax and logic. Syntax errors are easy to fix: the interpreter shows an error message pointing exactly to the location and error.

Traceback (most recent call last):
  File "python", line 7
    if chute == numero_secreto
                             ^
SyntaxError: invalid syntax

You missed two stitches, :, after the if.

Traceback (most recent call last):
  File "python", line 9
    else
       ^
SyntaxError: invalid syntax

Missed both points after the else.

With these fixes your code will not work yet, because the function return input will always be a string, then you’ll be comparing a string with an integer, which makes no sense and will never be true. As you want to read an integer, you need to convert the value as such by doing:

chute = int(input("Digite um numero:"))

However, this way, if the user enters with any value that is not numerical, an exception will be triggered. To solve this problem, read:

How to make the system display an error message when it is not number?

  • Okay, thanks for the knowledge

0

Apos if and Else put : ,and when using input and the expected result is an integer, use the int function()

Corrected:

print("**************")
print("Seja Bem Vindo")
print("**************")
numero_secreto = 65
chute = int(input("Digite um numero:"))
print("Você digitou: ",chute)
if numero_secreto == chute:
    print("você acertou")
else:
    print("Você errou, Tente novamente")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.