【发布时间】:2018-07-18 07:56:34
【问题描述】:
所以我有一个包含嵌套字典的列表,我想做的是从字典中读取视图的值并将它们通过乘数。
video_ads=({'title': 'Healthy Living', 'company': 'Health Promotion
Board','views': 15934},
{'title': 'Get a ride, anytime anywhere', 'company': 'Uber', 'views':
923834},
{'title': 'Send money with GrabPay', 'company': 'Grab', 'views': 23466},
{'title': 'Ubereats now delivers nationwide', 'company': 'Uber', 'views':
1337},
{'title': 'Get cabs now with UberFlash', 'company': 'Uber', 'views': 90234})
所以我要做的就是把标题和视图乘以某个因素,这就是我到目前为止所写的
def payment_for_views(x):
for x in range(0,5):
x = x+1
if video_ads[x]['views'] >= 50000:
payment = video_ads[x]['views']*0.55
elif views <50000 and video_ads[x]['views'] >=10000:
payment=video_ads[x]['views']*0.68
else:
payment = video_ads[x]['views']*0.82
#print(x)
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
print(video_ads[x]['title'] + ': $'+ str(payment))
但它似乎只打印一行,例如:
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
Get cabs now with UberFlash: $49628.700000000004
这就是我所得到的。我需要 5 行,它们都有不同的标题和不同的数量。帮助!!!并提前感谢!
【问题讨论】:
-
在 for 循环中缩进 print 语句。它只打印一个对象,因为
x = 5总是
标签: python python-3.x dictionary nested-loops nested-lists