将值分配给“名称”,然后更改值

时间:2019-03-04 05:14:25

标签: python-3.x

python的新手。我以为我会为租户/租户创建一个程序,在其中为您做一些简单的计算。但是,如何给一个人(名称)分配一个给定的值(租金),然后再使用一个函数将给定人(名称)的值(租金)更改呢?现在,它只是在列表中,但我想像在字典中那样分配值。有什么办法吗?

import numbers


class Tenant:
    def __init__(self, name, rent):
        self.name = name
        self.rent = int(rent)


tenantsAll = []
tenantsRent = []


def main():
    while True:                                  # Loop continuously
    inpNavn = input("Skriv navnet på dem som leier, avslutt med enter: ")   
    if inpNavn == "":                   # If it is a blank line...
        print(tenantsAll)
        return monthly()
    else:
        inpRent = input("Skriv hvor mye leiekostnadene er i mnd: ")
        userTenant = Tenant(inpNavn, inpRent)
        apName = userTenant.name
        apRent = userTenant.rent
        tenantsAll.append(apName)
        tenantsAll.append(apRent)
        tenantsRent.append(apRent)


def monthly():
    tenantsRent = [num for num in tenantsAll if isinstance(num, (int, float))]
    monthlyRevenue = sum(tenantsRent)
    yearlyRevenue = monthlyRevenue * 12
    print("Per måned:", monthlyRevenue, " Årlig", yearlyRevenue)
    moreTenants = input("Ønsker du flere som leier ut? '(y/n)' ")
    if moreTenants == 'y':
    return main()
 else:
    print("")

if __name__ == "__main__":
    main()

0 个答案:

没有答案