已知20個成績存入一個串列中(請注意,不是字典,沒有姓名,只有成績),請對其進行統計,輸出優(100~90)、良(89~80)、中(79~60)、差(59~0)四個等級的人數(在運行時輸入這20個成績,不是編程里)
求助..!
uj5u.com熱心網友回復:
def main(score_list):
better_count, good_count, middle_count, bad_count = 0, 0, 0, 0
for score in score_list:
if score >= 90 and score <= 100:
better_count += 1
elif score >= 80 and score <= 89:
good_count += 1
elif score >= 60 and score <= 79:
middle_count += 1
elif score >= 0 and score <= 59:
bad_count += 1
else:
print('{score}為無效成績')
print('輸入的成績中,優有{better_count}人,良有{good_count}人, 中有{middle_count}人, 差有{bad_count}人')
if __name__ == "__main__":
quit_input_flag = 1
input_list = list()
while quit_input_flag:
score = eval(input('請輸入成績(結束輸入請輸入q):'))
if score not is 'q':
input_list.append(score)
else:
quit_input_flag = 0
main(input_list)
可以參考一下,由于是在手機上打得代碼,可以存在縮進錯誤。
uj5u.com熱心網友回復:
非常感謝 但想問一下有沒有更簡潔的方法做這個題啊
uj5u.com熱心網友回復:
已知20個成績存入一個串列中(請注意,不是字典,沒有姓名,只有成績),請對其進行統計,輸出優(100~90)、良(89~80)、中(79~60)、差(59~0)四個等級的人數(在運行時輸入這20個成績,不是編程里)
求助..!
def main(score_list):
better_count, good_count, middle_count, bad_count = 0, 0, 0, 0
for score in score_list:
if score >= 90 and score <= 100:
better_count += 1
elif score >= 80 and score <= 89:
good_count += 1
elif score >= 60 and score <= 79:
middle_count += 1
elif score >= 0 and score <= 59:
bad_count += 1
else:
print('{score}為無效成績')
print('輸入的成績中,優有{better_count}人,良有{good_count}人, 中有{middle_count}人, 差有{bad_count}人')
if __name__ == "__main__":
quit_input_flag = 1
input_list = list()
while quit_input_flag:
score = eval(input('請輸入成績(結束輸入請輸入q):'))
if score not is 'q':
input_list.append(score)
else:
quit_input_flag = 0
main(input_list)
可以參考一下,由于是在手機上打得代碼,可以存在縮進錯誤。
非常感謝 但想問一下有沒有更簡潔的方法做這個題啊
main()函式里的話,暫時還沒,但是在輸入成績串列可以優化一下,變成這樣的
uj5u.com熱心網友回復:
已知20個成績存入一個串列中(請注意,不是字典,沒有姓名,只有成績),請對其進行統計,輸出優(100~90)、良(89~80)、中(79~60)、差(59~0)四個等級的人數(在運行時輸入這20個成績,不是編程里)
求助..!
def main(score_list):
better_count, good_count, middle_count, bad_count = 0, 0, 0, 0
for score in score_list:
if score >= 90 and score <= 100:
better_count += 1
elif score >= 80 and score <= 89:
good_count += 1
elif score >= 60 and score <= 79:
middle_count += 1
elif score >= 0 and score <= 59:
bad_count += 1
else:
print('{score}為無效成績')
print('輸入的成績中,優有{better_count}人,良有{good_count}人, 中有{middle_count}人, 差有{bad_count}人')
if __name__ == "__main__":
quit_input_flag = 1
input_list = list()
while quit_input_flag:
score = eval(input('請輸入成績(結束輸入請輸入q):'))
if score not is 'q':
input_list.append(score)
else:
quit_input_flag = 0
main(input_list)
可以參考一下,由于是在手機上打得代碼,可以存在縮進錯誤。
非常感謝 但想問一下有沒有更簡潔的方法做這個題啊
main()函式里的話,暫時還沒,但是在輸入成績串列可以優化一下,變成這樣的
if __name__ == "__main__":
input_list = eval(input('請輸入成績串列:'))
main(input_list)
uj5u.com熱心網友回復:
https://bbs.csdn.net/topics/396263713uj5u.com熱心網友回復:
def run(s_list):
count, ret = [101, 90, 80, 60, 0], [0, 0, 0, 0, 0] # 順序以此為優,良,中,差,非法
for i in range(len(s_list)): # s_list[i]代表的是成績
ret[4] = (ret[4] + 1)if (s_list[i] > 100 or s_list[i] < 0)else ret[4] # 算出非法值
for j in range(1, len(count)): # count[j]代表基準線
if count[j] <= s_list[i] < count[j-1]: # 如果成績在自己的基準線和前一個基準線之間
ret[j-1] = ret[j-1] + 1
break
print("優:" + str(ret[0]) + "個,",
"良:" + str(ret[1]) + "個,",
"中:" + str(ret[2]) + "個,",
"差:" + str(ret[3]) + "個,",
"非法:" + str(ret[4]) + "個")
if __name__ == "__main__":
input_list = []
while True:
s = input("請輸入第"+str(len(input_list) + 1)+"個的成績:")
if s == "q":
break
else:
input_list.append(eval(s))
run(input_list)
這個里面 核心塊只有七、八行 給你正解~
uj5u.com熱心網友回復:
已知20個成績存入一個串列中(請注意,不是字典,沒有姓名,只有成績),請對其進行統計,輸出優(100~90)、良(89~80)、中(79~60)、差(59~0)四個等級的人數(在運行時輸入這20個成績,不是編程里)
求助..!
def main(score_list):
better_count, good_count, middle_count, bad_count = 0, 0, 0, 0
for score in score_list:
if score >= 90 and score <= 100:
better_count += 1
elif score >= 80 and score <= 89:
good_count += 1
elif score >= 60 and score <= 79:
middle_count += 1
elif score >= 0 and score <= 59:
bad_count += 1
else:
print('{score}為無效成績')
print('輸入的成績中,優有{better_count}人,良有{good_count}人, 中有{middle_count}人, 差有{bad_count}人')
if __name__ == "__main__":
quit_input_flag = 1
input_list = list()
while quit_input_flag:
score = eval(input('請輸入成績(結束輸入請輸入q):'))
if score not is 'q':
input_list.append(score)
else:
quit_input_flag = 0
main(input_list)
可以參考一下,由于是在手機上打得代碼,可以存在縮進錯誤。
非常感謝 但想問一下有沒有更簡潔的方法做這個題啊
main()函式里的話,暫時還沒,但是在輸入成績串列可以優化一下,變成這樣的
if __name__ == "__main__":
input_list = eval(input('請輸入成績串列:'))
main(input_list)
還有什么函式可以做這個題
uj5u.com熱心網友回復:
def run(s_list):
count, ret = [101, 90, 80, 60, 0], [0, 0, 0, 0, 0] # 順序以此為優,良,中,差,非法
for i in range(len(s_list)): # s_list[i]代表的是成績
ret[4] = (ret[4] + 1)if (s_list[i] > 100 or s_list[i] < 0)else ret[4] # 算出非法值
for j in range(1, len(count)): # count[j]代表基準線
if count[j] <= s_list[i] < count[j-1]: # 如果成績在自己的基準線和前一個基準線之間
ret[j-1] = ret[j-1] + 1
break
print("優:" + str(ret[0]) + "個,",
"良:" + str(ret[1]) + "個,",
"中:" + str(ret[2]) + "個,",
"差:" + str(ret[3]) + "個,",
"非法:" + str(ret[4]) + "個")
if __name__ == "__main__":
input_list = []
while True:
s = input("請輸入第"+str(len(input_list) + 1)+"個的成績:")
if s == "q":
break
else:
input_list.append(eval(s))
run(input_list)
這個里面 核心塊只有七、八行 給你正解~
但不能只寫七八行呀

uj5u.com熱心網友回復:
你run就對了 能運行出正確的結果,解決需求 多少行其實沒必要的主要我看你上面嫌棄別人代碼行數太多而做的稍微一點點的修改
uj5u.com熱心網友回復:
def main(score_list):
score_level = {'優': range(90, 101), '良': range(80, 90), '中': range(60, 80), '差': range(0, 60)}
score_count = {'優': 0, '良': 0, '中': 0, '差': 0}
for score in score_list:
for key, value in score_level.items():
if score in value:
count = score_count.get(key) + 1
score_count.update({key: count})
break
return score_count
if __name__ == "__main__":
score_lst = list()
while True:
score_str = input('請輸入成績(結束請輸入q):')
if score_str == 'q':
break
score_lst.append(float(score_str))
print(main(score_lst))
uj5u.com熱心網友回復:
你run就對了 能運行出正確的結果,解決需求 多少行其實沒必要的
主要我看你上面嫌棄別人代碼行數太多而做的稍微一點點的修改
不是嫌棄…用詞謹慎點好嗎…
uj5u.com熱心網友回復:
我博客里面完整代碼轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/63793.html
上一篇:互聯網資料哪些可以爬取?
下一篇:正則運算式星號
