Cassinator
Cassinator

Reputation: 3

Syntax Error printing variables on Python

I am getting a syntax error on my Python code. The IDLE isn't giving any tips to where the error might be.

I am running Python 3 on a Raspberry Pi 3.

inches = input "How many inches?"
cm = inches*2.54
print "That is" {} "centimeters.".format(cm)

I expected the output to ask me how many inches I wanted to convert. It then would have stated the value of centimeters that it is equal to.

Instead, it comes up with a window that says "Syntax Error." and no other information.

Upvotes: 0

Views: 1390

Answers (4)

AcaNg
AcaNg

Reputation: 706

you must cover the string inside parenthesis.

inches = input("How many inches?")

but that's not enough, you need a number to perform multiplication operator. So cover your input() with float() for float number or int() for integer.

inches = float(input("How many inches?"))
# or
inches = int(input("How many inches?")) 

unlike python 2, in python 3, print() is a built-in function, its parameter must be put inside parenthesis. Also, brackets {} must be put in quotes.

print("That is {} centimeters.".format(cm))

So your code may look like:

inches = int(input("How many inches?")) # or inches = float(input("How many inches?")) 
cm = inches*2.54
print("That is {} centimeters.".format(cm))

Upvotes: 1

Steve
Steve

Reputation: 59

Double check your Python version on your Raspberry PI. F strings were introduced in 3.6 and if your PI is like mine the default Python version installed will be 3.5

Also print calls need parentheses e.g. print()

Upvotes: 0

Afik Friedberg
Afik Friedberg

Reputation: 342

inches = input("How many inches?")
cm = inches*2.54
print("That is" {} "centimeters.".format(cm))

Upvotes: 1

Xiidref
Xiidref

Reputation: 1521

The correct way to write this is

inches = input("How many inches?")
cm = inches*2.54
print("That is %f centimeters" % (cm))

The % means that you will insert a value here the character which follow id the type of the variable that you will insert here i use %f for float yout could also use %s for string for example.

Upvotes: 2

Related Questions