iT邦幫忙

0

while 迴圈!急!!

  • 分享至 

  • xImage

寫一段程式用while迴圈完成四科分數成績的加總集平均
分數:78
分數:65
分數:91
分數:53
總分=287.0 平均=71.75

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
海綿寶寶
iT邦大神 1 級 ‧ 2022-10-22 18:47:52

https://ithelp.ithome.com.tw/upload/images/20221022/20001787Iwis6iHxnk.png

sum = 0
avg = 0
cnt = 0
n = int(input("Enter a number (-1 to end):"))

while n!=-1:
    cnt = cnt+1
    sum = sum+n
    n = int(input("Enter a number (-1 to end):"))

avg=sum/cnt
print('sum=',sum,'average=',avg)
尼克 iT邦大師 1 級 ‧ 2022-10-23 14:37:12 檢舉

好心人!

好心人!

少兩個字
是好心情的
/images/emoticon/emoticon25.gif

0
Retex
iT邦新手 1 級 ‧ 2022-10-22 20:25:05
total = 0
avg = 0
i = 0

score = input("請依序輸入成績(空格分開): ")
score_list = list(map(float, score.split(' '))) # 將成績轉成列表

Len = len(score_list) # 成績數量
while i < Len:
    total += score_list[i] # 總分

avg = total / Len # 平均

print("總分={:.2f} 平均={:.2f}".format(total, avg)) # 取自小數點第2位

順便附上不用While用法:

total = 0
avg = 0

score = input("請依序輸入成績(空格分開): ")
score_list = list(map(float, score.split(' '))) # 將成績轉成列表

total = sum(score_list) # 總分
avg = total / len(score_list) # 平均

print("總分={:.2f} 平均={:.2f}".format(total, avg)) # 取自小數點第2位
0
qq_jeremy
iT邦見習生 ‧ 2022-10-27 14:27:01
times_count = 0
total = 0

while True:
    n = int(input("請輸入分數,輸入'-1'結束填入 => "))
    if n == -1:
        break
    else:
        total += n
        times_count += 1

print("總分 =>",float(total),"\t平均 =>",total/times_count)

https://ithelp.ithome.com.tw/upload/images/20221027/20154426rZ2hG5HA3S.png

float(total) 只是為了要符合你總分使用浮點數的需求,可選擇要不要轉型。

0
shiaobin
iT邦新手 4 級 ‧ 2022-10-27 15:13:16

☑用while迴圈
☑四科分數加總
☑四科分數平均

do_your_homework_by_yourself = True
while do_your_homework_by_yourself:
    my_scores = [78, 65, 91, 53]
    my_sum = sum(my_scores)
    my_avg = my_sum / len(my_scores)
    print(
        f"""分數:{my_scores[0]}
分數:{my_scores[1]}
分數:{my_scores[2]}
分數:{my_scores[3]}
總分={my_sum} 平均={my_avg}"""
    )
    do_your_homework_by_yourself = False

bennett iT邦新手 3 級 ‧ 2022-10-28 14:13:48 檢舉

❎do_your_homework_by_yorself

shiaobin iT邦新手 4 級 ‧ 2022-10-28 18:42:06 檢舉

謝謝幫忙抓錯字。

我要發表回答

立即登入回答