Home > Blockchain >  What am I doing wrong in this simple Python code?
What am I doing wrong in this simple Python code?

Time:04-11

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

CodePudding user response:

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

CodePudding user response:

You need to get some practice handling strings and numbers and converting between the two. Note for example that input() always gives you strings, whereas doing maths always needs numbers.

So there are a few things going on here:

  1. Compare unit to "C" (a string), not C (a variable).
  2. Before trying to do maths with temp, convert it to a number with float(temp). (Don't use int() because if someone enters 21.9 (say), int will round down.
  3. When you construct a string with , all the elements must be strings. So convert numbers back to strings with str(num).

And be careful with the order of operations in your formula!

CodePudding user response:

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

CodePudding user response:

In the condition, you have used C and F which are interpreted as variables, you should use " marks ("F", "C"). Also for printing you should pass string to the print function, so convert your int variables ( "item" and F and C) to strings using str()

  • Related