Accademia:Mortgage pt

Versione del 22 mar 2011 alle 11:55 di Risca86 (discussione | contributi) (Creata pagina con '{{Template:Acc_Menu_Python}} <pre> #!/usr/bin/env python3 def mortgage(loan_amount, years, instalments_for_year, rate): """Return a dictonary containing data taken from the...')
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
#!/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)