Python嵌套循环: 错误的输出

huangapple go评论94阅读模式
英文:

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 = [[
    &#39;Facebook&#39;, 3.5],
    [&#39;Notion&#39;, 4.0],
    [&#39;Astropad Standard&#39;, 4.5],
    [&#39;NAVIGON Europe&#39;, 3.5
]]

for app_ratings in app:
    app=[1]
    if app_ratings &lt; 3.0:
        app.append(&#39;below average&#39;)

    elif   3.0 &lt;= app_ratings &lt; 4.0:        
        app.append(&#39;roughly average&#39;)

    elif app_ratings &gt;= 4.0:
        app.append(&#39;better than average&#39;)

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 = [[&#39;Facebook&#39;, 3.5],
    [&#39;Notion&#39;, 4.0],
    [&#39;Astropad Standard&#39;, 4.5],
    [&#39;NAVIGON Europe&#39;, 3.5]
]

for app in app_ratings:
#app[1] is the score (3.5, 4.0,...)
#app is each element inside app_ratings ([&#39;Facebook&#39;, 3.5],...)
    if app[1] &lt; 3.0: 
        app.append(&#39;below average&#39;)
    elif app[1] &lt; 4.0:
        app.append(&#39;roughly average&#39;)
    else:
        app.append(&#39;better than average&#39;)

print(app_ratings)
#[    [&#39;Facebook&#39;, 3.5, &#39;roughly average&#39;],
#    [&#39;Notion&#39;, 4.0, &#39;better than average&#39;],
#    [&#39;Astropad Standard&#39;, 4.5, &#39;better than average&#39;],
#    [&#39;NAVIGON Europe&#39;, 3.5, &#39;roughly average&#39;]
#]

huangapple
  • 本文由 发表于 2023年3月7日 09:31:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定