2
votes

I am very new to Python but very interested in learning. For a uni Python course I am to create a program that converts a temp to Celsius or Fahrenheit, and displays the conversion to the first decimal place. My issue lies in my print lines.

As you can see in my code I have attempted to use the round() function while concatenating. Although I have attempted to convert the would be float to a str(), when I run my program, i am given the following error.

TypeError: type str doesn't define round method

for the following line

print (round(str(temp), 1)+ " degrees celsius = " + str(degF) + " degrees Fahrenheit. ")

and similarly for the print line in my elif bloc. Any help understanding and solving the issue would be greatly appreciated.

temp=float(input("Enter a temperature value to convert: "))
unit=str(input("Convert to Fahrenheit or Celsius? Enter f or c: "))

if unit=="c" or unit == "C":
    degC=(temp)
    temp=(1.8*temp)+32
    print (round(str(temp), 1) + " degrees fahrenheit = " + str(degC) + " degrees Celsius. ")
elif unit=="f" or unit == "F":
    degF=(temp)
    temp=(temp-32)/1.8
    print (round(str(temp), 1)+ " degrees celsius = " + str(degF) + " degrees Fahrenheit. ")
else:
    print("you did not enter an f or c. Goodbye ")

My outputs should be rounded to the first decimal place.

3

3 Answers

1
votes

Don't convert temp to a string. Keep it as a float for round. Turn the resultant after round into a str for concatenation

str(round(temp, 1))
0
votes

Your operations are in the wrong order. Round first, then convert to string:

print (str(round(temp), 1)+ " degrees celsius = " ...
0
votes

Unless this is a classroom assignment to use the round function, I don't think the round function is needed.

print ("%.1f degrees Celsius = %.1f degrees Fahrenheit" % (temp, degC))