0

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.

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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

1 Comment

Check the quotes in print statement and wrap input into int or float as it is used for multiplication.
0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.