— 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.
JavaScript
x
10
10
1
print("Lets convert from C to F/ F to C")
2
temp = int(input("enter the temperature: "))
3
unit = input("enter the unit (C or F)")
4
if unit == C:
5
F = temp * 1.8 + 32
6
print(temp +"C is equal to"+F +"F")
7
elif unit == F:
8
C = temp / 1.8 - 32
9
print(temp + "F is equal to" +C + "C")
10
Advertisement
Answer
You might want to pay more attention to strings.
- use string literals
'F'
and'C'
(not bareF
andC
) in theif
conditions. - explicitly convert
int
tostr
by usingstr()
, when you concatenate anint
andstr
. Or better, use f-string.
JavaScript
1
10
10
1
print("Lets convert from C to F/ F to C")
2
temp = int(input("enter the temperature: "))
3
unit = input("enter the unit (C or F)")
4
if unit == 'C':
5
F = temp * 1.8 + 32
6
print(str(temp) +"C is equal to"+str(F) +"F")
7
elif unit == 'F':
8
C = temp / 1.8 - 32
9
print(str(temp) + "F is equal to" +str(C) + "C")
10
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:
JavaScript
1
10
10
1
print("Let's convert from °C to °F or °F to °C!")
2
temp = int(input("Enter the temperature: "))
3
unit = input("Enter the unit (C or F): ")
4
if unit.upper() == 'C':
5
F = temp * 1.8 + 32
6
print(f"{temp}°C is equal to {F}°F")
7
elif unit.upper() == 'F':
8
C = (temp - 32) / 1.8
9
print(f"{temp}°F is equal to {C}°C")
10