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!
you have a bug dude... its pretty simple
if toScale.lower() == "K":
SHOULD BE
if toScale.lower() == "k":
but try this:
if toScale in ["K", "k"]:
You're using lower()
but comparing with an upper-case character. So consider using if toScale.lower() == "k":
or if toScale.upper() == "K":
.
将toScale.lower() == "K"
更改为toScale.lower() == "k"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.