所以我是python的新手,并且在python中遇到了这个问题,说有5个输入
Andy 500
Bobby 200
Cindy 100
Daria 400
Elise 300
他们互相捐了钱
Andy gives 100 to Bobby
Bobby gives 50 to Cindy
Cindy gives 25 to Daria
Elise gives 100 to Cindy
Daria gives 75 to Andy
输出应如下所示
Andy 475
Bobby 150
Cindy 225
Daria 350
Elise 200
这是我到目前为止提出的代码
#initMoney = [500, 200, 100, 400, 300]
initMoney = [
{'Andy': 500},
{'Bobby': 200},
{'Cindy': 100},
{'Daria': 400},
{'Elise': 100}
]
loanMoney = [100, 50, 25, 75, 100]
def loanBucks(init, loan):
return init - loan
result = list(map(loanBucks, initMoney, loanMoney))
print(result)
print(result[0])
initMoney[0] += loanMoney[3]
initMoney[2] += loanMoney[1] + loanMoney[0]
initMoney[3] += loanMoney[2]
答案 0 :(得分:1)
您可以使用词典来存储不同人的钱。
关于代表交易,我建议列出词典。
我将不会编写用于输入姓名和货币以及用于输入交易的代码,因为我确信您可以管理此代码。
以下是用于定义货币和交易以及处理交易的代码:
people = {
'Andy': 500,
'Bobby': 200,
'Cindy': 100,
'Daria': 400,
'Elise': 300
}
transactions = [
{'from': 'Andy', 'to': 'Bobby', 'amount': 100},
{'from': 'Bobby', 'to': 'Cindy', 'amount': 50},
{'from': 'Cindy', 'to': 'Daria', 'amount': 25},
{'from': 'Elise', 'to': 'Cindy', 'amount': 100},
{'from': 'Daria', 'to': 'Andy', 'amount': 75}
]
for transaction in transactions:
people[transaction['from']] -= transaction['amount']
people[transaction['to']] += transaction['amount']
for person, money in people.items():
print(person, money)
给出:
Andy 475
Bobby 250
Cindy 225
Daria 350
Elise 200
答案 1 :(得分:0)
您要解决的问题非常适合学习面向对象的python(使用具有exchange_money()之类的属性的类来实现。)
无论如何以自己的方式解决此问题,您都可以使自己成为一个功能,这会更加优雅。
List<Table> tablesList = ...
Stream<Table> usableTables = tablesList.stream().filter(Table::isUsable);