Accademia:Mortgage pt

Da Guide@Debianizzati.Org.
Vai alla navigazione Vai alla ricerca
#!/usr/bin/env python3

def mortgage(loan_amount, years, instalments_for_year, rate):
    """Return a dictonary containing data taken from the user."""
    return {'LoanAmount':loan_amount, 'Years':years, 'InstalmentsForYear':instalments_for_year, 'Rate':rate}

def ask(question, object_type):
    """Return the value if the input is correct"""
    value = None
    while value == None:
        try:
            value = object_type(input(question + ": >"))
        except ValueError:
            print("You have to insert a number!")
    return value

def mortgage2(): # usando il metodo keys di dict si fa ancora meglio
    mortgage = {}
    name = ["LoanAmount", "Years", "InstalmentsForYear", "Rate"]
    i = 0
    while i < len(name):
        mortgage[name[i]] = ask("Please insert the " + name[i])
        i += 1
    return mortgage

if __name__ == '__main__':
    loan_amount = ask('Loan amount', int)
    years = ask('Years', int)
    instalments_for_year = ask('Instalments for year', int)
    rate = ask('Rate', float)
    my_mortgage = mortgage(loan_amount, years, instalments_for_year, rate)
    print(my_mortgage)