英文:
Python nested loop: wrong output
问题
以下是翻译好的部分:
任务是完成一段代码,根据应用的评分将每个应用标记为“低于平均水平”、“大致平均水平”或“高于平均水平”。在for循环内,
如果应用的评分小于3.0,则通过将字符串'below average'追加到当前迭代变量来将应用标记为“低于平均水平”。如果应用的评分大于或等于3.0且小于4.0,则通过将字符串'roughly average'追加到当前迭代变量来将应用标记为“大致平均水平”。如果应用的评分大于或等于4.0,则通过将字符串'better than average'追加到当前迭代变量来将应用标记为“高于平均水平”。打印app_ratings以查看结果。
这是我的代码:
app=[]
app_ratings = [[
'Facebook', 3.5],
['Notion', 4.0],
['Astropad Standard', 4.5],
['NAVIGON Europe', 3.5
]]
for app_rating in app_ratings:
app=[1]
if app_rating[1] < 3.0:
app.append('below average')
elif 3.0 <= app_rating[1] < 4.0:
app.append('roughly average')
elif app_rating[1] >= 4.0:
app.append('better than average')
print(app_ratings)
请指导我如何正确迭代这个嵌套列表以获得正确的输出。
英文:
The task is to complete a code to label each app as "below average", "roughly average", or "better than average" depending on its rating. Inside the for loop,
If the app rating is less than 3.0, then label the app as "below average" by appending the string 'below average' to the current iteration variable. Else if the app rating is greater than or equal to 3.0 and below 4.0, then label the app as "roughly average" by appending the string 'roughly average' to the current iteration variable. Else if the app rating is greater than or equal to 4.0 label the app "better than average" by appending the string 'better than average' to the current iteration variable. Print app_ratings to see the results.
This is my code:
app=[]
app_ratings = [[
'Facebook', 3.5],
['Notion', 4.0],
['Astropad Standard', 4.5],
['NAVIGON Europe', 3.5
]]
for app_ratings in app:
app=[1]
if app_ratings < 3.0:
app.append('below average')
elif 3.0 <= app_ratings < 4.0:
app.append('roughly average')
elif app_ratings >= 4.0:
app.append('better than average')
print(app_ratings)
Kindly advise on how I can correctly iterate over this nested list to achieve the correct output.
答案1
得分: 0
我认为下面的代码是你想要的。我认为你想要将"average"字符串附加到app_ratings列表中每个元素的末尾。
app_ratings = [['Facebook', 3.5],
['Notion', 4.0],
['Astropad Standard', 4.5],
['NAVIGON Europe', 3.5]
]
for app in app_ratings:
# app[1] is the score (3.5, 4.0,...)
# app is each element inside app_ratings (['Facebook', 3.5],...)
if app[1] < 3.0:
app.append('below average')
elif app[1] < 4.0:
app.append('roughly average')
else:
app.append('better than average')
print(app_ratings)
# [['Facebook', 3.5, 'roughly average'],
# ['Notion', 4.0, 'better than average'],
# ['Astropad Standard', 4.5, 'better than average'],
# ['NAVIGON Europe', 3.5, 'roughly average']]
英文:
I think the below code is what you want. I think you want to append the 'average' string to the end of each element inside app_ratings list.
app_ratings = [['Facebook', 3.5],
['Notion', 4.0],
['Astropad Standard', 4.5],
['NAVIGON Europe', 3.5]
]
for app in app_ratings:
#app[1] is the score (3.5, 4.0,...)
#app is each element inside app_ratings (['Facebook', 3.5],...)
if app[1] < 3.0:
app.append('below average')
elif app[1] < 4.0:
app.append('roughly average')
else:
app.append('better than average')
print(app_ratings)
#[ ['Facebook', 3.5, 'roughly average'],
# ['Notion', 4.0, 'better than average'],
# ['Astropad Standard', 4.5, 'better than average'],
# ['NAVIGON Europe', 3.5, 'roughly average']
#]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论