简体   繁体   中英

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. It can be called multiple times to accept input from more users. Just append the list to the main list.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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