![](/img/trans.png)
[英]How do I find ALL the positions of the lowest numbers in a list? (python)
[英]Python: How do I take user input and find the highest and lowest numbers?
我需要編寫一個程序,從用戶給出的輸入中找到最高值和最低值。 我有一個程序,它接受用戶給出的數字和名稱,並將它們分類成這樣的列表:
[['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score']]
在取出最高和最低的分數之前,我需要知道如何獲取這個列表並閱讀每個分數。 然后我需要一個程序來獲取所有這些分數並獲得所有分數的平均值。
任何幫助是極大的贊賞。
這個 function 將要求用戶輸入並返回名稱、分數的列表。 可以多次調用它來接受更多用戶的輸入。 只是 append 列表到主列表。
def get_input():
'''append this list to the main list that is holding the user inputs'''
name, score = input("what is your name"), input("what is your score")
return [name, float(score)]
如果需要將第二部分放入 function 中,您可以隨意組合。 我希望我不只是為你做作業...
sorted_list = sorted(a_list, key=lambda _: _[1], reverse=True)
high_score, low_score = sorted_list[0], sorted_list[-1]
avg_score = sum([_[1] for _ in a_list]) / len(a_list)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.