python code help

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

python code help

Post by krona64 on Sat Mar 01, 2014 7:56 pm
([msg=79705]see python code help[/msg])

Im having trouble with my code and I don't understand what I am doing wrong. my code runs but if the input is valid, it exits the program. if the input is invalid it continues through the 3 attempts then exits. I confused where i went wrong and why its closing in the input is valid/true.

any help would be greatly appreciated.




Code: Select all
def miles_to_km(miles):
    return miles * 1.6

def read_miles(prompt):
    miles = float(input(prompt))
    if miles < 0: raise ValueError()
    return miles

def read_miles_retry(prompt, retries = 3):
    while retries > 0:
        try:
            return read_miles(prompt)
        except:
            retries -= 1
            print('Invalid input')

    raise ValueError()


#---------------------------------------------------------------

def fah_to_cel(temp):
    return (temp - 32) * 5/9

def read_temp(prompt):
    temp = float(input(prompt))
    if temp > 1000: raise ValueError()
    return temp

def read_temp_retry(prompt, retries = 3):
    while retries > 0:
        try:
            return read_temp(prompt)
        except:
            retries -= 1
            print('Invalid input')

    raise ValueError()

#----------------------------------------------------------------

def gal_to_lit(gallon):
    return gallon * 3.9

def read_gallon(prompt):
    gallon = float(input(prompt))
    if gal < 0 : raise ValueError()
    return gallon

def read_gallon_retry(prompt, retries = 3):
    while retries > 0:
        try:
            return gallon(prompt)
        except:
            retries -= 1
            print('Invalid input')

    raise ValueError()

#-------------------------------------------------------------------

def miletokm():
    try:
        mtk = miles_to_km(read_miles_retry('how many miles would\
you like to convert? '))
        print( format (mtk, '.2f'))
    except:
       exit()



def fahtocel():
    try:
        ftc = fah_to_cel(read_temp_retry('how many degrees in Fahrenhiet would\
you like to convert to celsius? '))
        print( format (ftc, '.1f'))
    except:
        exit()



def gallontoliter():
    try:
        gtl = gal_to_lit(read_gallon_retry('how many gallons would you\
like to convert to liter? '))
        print( format (gtl, '.2f'))
    except:
        exit()


def main():

    miletokm()

    fahtocel()

    gallontoliter()

    exit()


main()
Last edited by krona64 on Sun Mar 02, 2014 1:10 pm, edited 2 times in total.
krona64
New User
New User
 
Posts: 18
Joined: Thu Mar 21, 2013 12:20 am
Blog: View Blog (0)


Re: python code help

Post by parakkafaith on Sat Mar 01, 2014 9:34 pm
([msg=79707]see Re: python code help[/msg])

On line 74, "temp_retry" is not defined.

It seems that you're defining "read_temp_retry" on line 30 and referring to it on line 74 with the wrong name, which is making your code cry and kill itself just like you told it to!

Edit: Perhaps I'm missing something here, but the main() functions seem rather inconsistent in their calling on your retry functions.

Is there a reason you went with this:

read_miles_retry
temp_retry
gallon_retry


as opposed to this?:

read_miles_retry or miles_retry
read_temp_retry or temp_retry
read_gallon_retry or gallon_retry
User avatar
parakkafaith
Experienced User
Experienced User
 
Posts: 57
Joined: Fri Jan 07, 2011 10:59 pm
Blog: View Blog (0)


Re: python code help

Post by krona64 on Sun Mar 02, 2014 1:19 pm
([msg=79711]see Re: python code help[/msg])

thanks for your help.
krona64
New User
New User
 
Posts: 18
Joined: Thu Mar 21, 2013 12:20 am
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests