简体   繁体   English

Python:如何获取用户输入并找到最高和最低数字?

[英]Python: How do I take user input and find the highest and lowest numbers?

I need to write a program that finds the highest and lowest values from the input given by the user.我需要编写一个程序,从用户给出的输入中找到最高值和最低值。 I have a program that takes the numbers and names given by the user and sorts them into a list like this:我有一个程序,它接受用户给出的数字和名称,并将它们分类成这样的列表:

[['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score'], ['Name', 'Score']]

I need to know how to take this list and read each score, before pulling out the highest and lowest one.在取出最高和最低的分数之前,我需要知道如何获取这个列表并阅读每个分数。 Then I need a program that takes all of those scores and gets the mean average of all of them.然后我需要一个程序来获取所有这些分数并获得所有分数的平均值。

Any help is greatly appreciated.任何帮助是极大的赞赏。

This function would ask for user inputs and return a list of name, score.这个 function 将要求用户输入并返回名称、分数的列表。 It can be called multiple times to accept input from more users.可以多次调用它来接受更多用户的输入。 Just append the list to the main list.只是 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)]

If the second part needs to be put in a function, you can put that together as you please.如果需要将第二部分放入 function 中,您可以随意组合。 I hope I didn't just do your homework for you...我希望我不只是为你做作业...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM