1

i tried to use this code to make a simple temp converter between Celcius and Farenheit....My code :

value=raw_input("Temperature Reading= ")
check=value[-1]
c=int(value[:-1])
if check =='c' or 'C':
    print (9*c/5)+32,'F'
if check =='f' or 'F':
    print 5*(c-32)/9,'C'

raw_input("Press <Enter> to exit!")

the problem with this is that if the user inputs for example 50f it prints both of them in the 'if' functions. I need to correct it. thanks :)

iNuwanS
  • 99
  • 1
  • 1
  • 6
  • 2
    The first thing that comes to mind is to use ```regex``` to split the letters from numbers. That's probably the most general/robust way to do it. – wnnmaw Apr 01 '14 at 18:21
  • i have no idea of such complex functions '^.^ my bad i know.... – iNuwanS Apr 01 '14 at 18:22
  • 4
    We get this question daily. `or` doesn't work that way, use `if check =='c' or check == 'C':` or `if check in 'cC':` or `if check.lower() == 'c':`... – RemcoGerlich Apr 01 '14 at 18:23
  • @RemcoGerlich thanks i think i got it xD so OR doesnt work that way :) – iNuwanS Apr 01 '14 at 18:26

1 Answers1

3

Your checks are not correct

if check =='f' or 'F':

should be

if check == 'f' or check == 'F':

Idem for the other 'C' one.

Also nicer is:

if check in ['c', 'C']:

Or

if check.lower() == 'c':
RickyA
  • 15,465
  • 5
  • 71
  • 95