samedi 19 septembre 2015

python - def - else statement not working

Okay, I'm very new to this, wrote a simple unit conversion program from miles to km however my else statement doesn't seem to be working.

Here's what wrote:

def kmtoMil(dist):
   return (dist / 0.62137)

def miltoKm(dist):
   return (dist * 0.62137)

def convert(dist, toScale):
   if toScale.lower() == "K":
      return miltoKm(dist)
   else:
      return kmtoMil(dist)

print ("Enter a distance: ")
dist = int(input())
print ("Choose unit to convert to: ")
scale = input()
convertDist = convert(dist, scale)
print (dist, convertDist, scale)

It seems to apply the first def (I can interchange the kmtoMil & miltoKm on line 9 & 11 and 9 seems to take) but not the second. I hope that makes sense.

I'm thinking something wrong with the else: statement...but not sure and curious.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire