mardi 30 juin 2015

Why isn't the function being called in the if statement? (python)

I have recently begun learning programming and more specifically python. I am a total beginner and I started working on my first small project of converting inches and pounds. I can't figure out why the function isn't being called in the if statement. Please help! Thanks.

# Convert inches to centimeters and pounds to kilograms


# Converts inches to centimeters
def convic():
    amti = raw_input("How many inches? **Integers only!** ")
    if amti.isdigit():
        amti = int(float(amti))
        centi = amti * 2.54
        print "%d inches equals %d centimeters." % (amti, centi)
    else:
        print "Sorry that is not a number and/or integer."

# Converts pounds to kilograms  
def convpk():   
    amtp = raw_input("How many pounds? **Integers only!** ")
    if amtp.isdigit():
        amtp = int(float(amtp))
        kilo = amtp * 0.45359
        print "%d pounds equals %d kilograms." % (amtp, kilo)
    else:
        print "Sorry that is not a number and/or integer."

answer = raw_input("Would you like to convert inches to centimeters or    pounds to kilograms? ")   
if answer == "inches to centimenters":
    convic()
if answer == "pounds to kilograms":
    convpk()

# Not a correct response        
else:
    while answer != "inches to centimeters" and "pounds to kilograms":
        answer = raw_input("Sorry please enter inches to centimeters or  pounds to kilograms. ")
        if answer == "inches to centimenters":
            convic()
        if answer == "pounds to kilograms":
            convpk()

Aucun commentaire:

Enregistrer un commentaire