-1

-- My goal is to make a code where user has to enter temperature, then user has to enter unit (C or F), then I wanna show conversion to F if users type C and vice versa. what wrong here tell me please I am super new to python and learning it all day, thanks.

print("Lets convert from C to F/ F to C")
temp = int(input("enter the temperature: "))
unit = input("enter the unit (C or F)")
if unit == C:
 F = temp * 1.8 + 32
 print(temp +"C is equal to"+F +"F")
elif unit == F:
 C = temp / 1.8 - 32
 print(temp + "F is equal to" +C + "C")
asked Apr 10, 2022 at 22:33
3
  • 2
    what is the error you are getting? Commented Apr 10, 2022 at 22:34
  • 1
    You are presently comparing unit with the contents of a variable C rather than the string 'C'. You need to change your condition to if unit == 'C' and the same for F. Commented Apr 10, 2022 at 22:36
  • C and F are not declared variables anywhere. What you want it to compare to the string "C" or "F". Commented Apr 10, 2022 at 22:36

2 Answers 2

2

You might want to pay more attention to strings.

  1. use string literals 'F' and 'C' (not bare F and C) in the if conditions.
  2. explicitly convert int to str by using str(), when you concatenate an int and str. Or better, use f-string.
print("Lets convert from C to F/ F to C")
temp = int(input("enter the temperature: "))
unit = input("enter the unit (C or F)")
if unit == 'C':
 F = temp * 1.8 + 32
 print(str(temp) +"C is equal to"+str(F) +"F")
elif unit == 'F':
 C = temp / 1.8 - 32
 print(str(temp) + "F is equal to" +str(C) + "C")

By the way, I am afraid the formula is a little bit off. Another version, in which I corrected the formula and used f-string, is as follows:

print("Let's convert from °C to °F or °F to °C!")
temp = int(input("Enter the temperature: "))
unit = input("Enter the unit (C or F): ")
if unit.upper() == 'C':
 F = temp * 1.8 + 32
 print(f"{temp}°C is equal to {F}°F")
elif unit.upper() == 'F':
 C = (temp - 32) / 1.8
 print(f"{temp}°F is equal to {C}°C")
answered Apr 10, 2022 at 22:37
Sign up to request clarification or add additional context in comments.

1 Comment

That's a good catch. An example of such an f string would be: print(f"{temp} C is equal to {F} F")
0
print("Lets convert from C to F/ F to C")
temp = int(input("enter the temperature: "))
unit = input("enter the unit (C or F)")
if unit == "C": #small typo
 F = temp * 1.8 + 32
 print(str(temp) + "C is equal to" + str(F) + "F") # you cant do int + str
elif unit == "F":
 C = temp / 1.8 - 32
 print(str(temp) + "F is equal to" + str(C) + "C")
answered Apr 10, 2022 at 22:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.