0

I'm brand new to python and my teacher showed me an example on how to write multiple variables in a print function but I am getting a syntax error. Did I write it wrong? How can I fix this?

def fahrenheit_to_celcius(num):
    celciusTemp=(num-32)*0.556
    print("When you convert %s to celcius the result is %s",%(num, 
celciusTemp))

Above is just a simple function I'm writing as part of some beginner exercise. But when I run the code it says my print function has invalid syntax.

1
  • print("when you convert {} to celcius the result is {:f}".format(num,celciusTemp)) Commented Mar 23, 2019 at 1:21

5 Answers 5

2

Use this code.

def fahrenheit_to_celcius(num):
    celciusTemp=(num-32)*0.556
    print("When you convert %s to celcius the result is %s" % (num, 
celciusTemp))
Sign up to request clarification or add additional context in comments.

Comments

1

Because you seem to be working in Python 3, there is a really cool way to format strings that I absolutely love. With this method, your print() would look like

print(F"When you convert {num} to Celsius, the result is
{celsiusTemp}.")

With this method, you can write pretty much any python in between {} and python automatically puts the returned output of that code in your string.

Comments

1

Here I fixed your code. I also added a few lines of extra code so that it is a bit more readable for your prof. It should be easy enough for you to understand. Here's the code:

def fahrenheit_to_celcius(num):
    celciusTemp=(num-32)*0.556
    print("When you convert {} fahrenheit to celcius the result is {:f}".format(num,celciusTemp))
num = int(input("Input the degrees of fahrenheit you want to convert to celcius:"))
fahrenheit_to_celcius(num)

Hope it helps :)

Comments

0

You can use Format. In your case. :f (Displays fixed point number (Default: 6))

string = "when you convert {} to celcius the result is {:f}".format(num,celciusTemp)
print(string) 

More information please check this link https://www.programiz.com/python-programming/methods/string/format

Comments

-1

Remove the comma before the percent sign.

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.