Syntax = Invalid Syntax for variable name

Asked

Viewed 19,135 times

1

LadodoQuadrado = input("Digite o valor correspondente ao lado de um quadrado: "
variavel = float(LadodoQuadrado)

x = LadodoQuadrado * 4

y = LadodoQuadrado ^ 2

print("perímetro:", x, "área:", y)

When I have run the program, it says "Syntaxerror: invalid syntax" in the variables, each hour changes the variable that gives error. Someone can help me?

2 answers

1


This syntax error is because a parenthesis is missing at the end of input.

After solving this error, the script will still return another error

Typeerror: Unsupported operand type(s) for : str and 'int'

This is because the return of input is a string, that is, you need to convert the user input to a number and only then try to do some mathematical operation with the same.

I put a direct conversion using int(input("")), however, keep in mind that this will cause an error if the user enters any value that is not exactly a number (if the input is "Walkyrien", for example).

LadodoQuadrado = int(input("Digite o valor correspondente ao lado de um quadrado: "))
variavel = float(LadodoQuadrado)

x = LadodoQuadrado * 4

y = LadodoQuadrado ^ 2

print("perímetro:", x, "área:", y)

For knowledge purposes, a way to do this by treating user input

strEntrada = input("Digite o valor correspondente ao lado de um quadrado: ")

try:
    LadodoQuadrado = int(strEntrada)
except ValueError:
    print('entre com um número inteiro')
    exit()

variavel = float(LadodoQuadrado)

x = LadodoQuadrado * 4

y = LadodoQuadrado ^ 2

print("perímetro:", x, "área:", y)

0

The potentiation operator in Python is not " ", but "**" or Pow (x, y) if you want to use the Math library function.

Browser other questions tagged

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