Python 題目

PROBLEM

list comprehension(mapping映照)

#python #程式碼

scores = (50,60,60,40,80)

# 1 if s >=60 else 0 表示如果為真=1,否=0, for s in scores 表示 s 是 scores裡面的內容
p = [1 if s >= 60 else 0 for s in scores]
#    真____條件____否

print(p)
print(sum(p))

example:

chinese = int(input())
english = int(input())
math = int(input())

total = chinese + english + math
avg = total / 3

cnt = 0

if sum([1 if s >= 90 else 0 for s in (chinese,english,math)]) >= 2:
  rank = "A"
else:
  rank = "B"

print(f'總分:{total}, 平均:{avg:.0f}, 等級:{rank}')