0

i have an exercise that asks me to build a temperature converter. i need to ask a user what temperature he needs to convert then by the last letter c or f i will convert the temperature.

we didnt learn yet how to use def: , i was asked to use only if and elif statements. this is one of the things i tried but nothing seems to work for me:

temp = (input('enter a temperature you would like to convert: ')

if temp[-1] == 'c':
    print(float(temp -32)/ 1.8) + 'f'
elif temp[-1] == 'f':
    print(float(temp * 1.8) + 32) + 'c'

1 Answers1

0

It should be like this:

temp = input('enter a temperature you would like to convert: ')

if temp[-1] == 'c':
    print(str((float(temp[:-1]) * (9/5)) + 32) + 'f')
elif temp[-1] == 'f':
    print(str((float(temp[:-1]) + 32) * (5/9) ) + 'c')

The conversion formulas you used are not correct. here are the correct ones:

Input 11

  • F to C: (11°F − 32) × 5/9 = -11.67°C
  • C to F: (11°C × 9/5) + 32 = 51.8°F
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45