简体   繁体   中英

Python - lists of lists and user input (user selects position from a bigger list, then gets print of an f string with elements of a chosen sub - list)

this is my first question here ever, I am on the very beginnin of my programming course. I really tried to find a solution myself and then look for something similar in the internet, but it seems I don't even know how to name the (extremely basic) problem properly, so google search gives me no results.

I have a task to do. Part of that task is: I have a list of lists, which is a list of richest people in the world and additional data about them. It has 101 positions and looks like this:

[['Rank', 'Name', 'Total Net Worth', '$ Last Change', '$ YTD Change', 'Country', 'Industry'], ['1', 'Jeff Bezos', '$188B', '+$1.68B', '-$2.31B\xa0', 'United States', 'Technology'], 
['2', 'Elon Musk', '$170B', '-$2.89B', '+$773M\xa0', 'United States',

and so on. My goal is to ask user for int input (a number between 1 and 100) and then display him data of a person who has that position in the ranking, like this:

position = int(input("Hello, this is Top 100 Richest People List. Press number "\
f"from 1 to 100 to see what billionaire is on that position in the ranking."))

#stuff happens#
    
print(f"The billionaire with a position number ... is ... . Total Net Worth   "\
f"amounts to ... $. Last change amounts to ...$. YTD change is ... .          "\
f"The country of origin is ... and the industry is ... .") 

I have no idea how to map user input into the selection from a list, and then print the content just of that sub - list in the f string. So I kindly ask for some help, hints, or at least some tutorial "lists and lists + user inputs" which would be useful.

Thank you

Is this what you're looking for?

l = [['Rank', 'Name', 'Total Net Worth', '$ Last Change', '$ YTD Change', 'Country', 'Industry'], ['1', 'Jeff Bezos', '$188B', '+$1.68B', '-$2.31B\xa0', 'United States', 'Technology'], 
['2', 'Elon Musk', '$170B', '-$2.89B', '+$773M\xa0', 'United States', ...

position = int(input("Hello, this is Top 100 Richest People List. Press number "\
f"from 1 to 100 to see what billionaire is on that position in the ranking."))

try:
 found_person = l[position]
 
 print(f"The billionaire with a position number {position} is {found_person[1]} . Total Net Worth  
 "\
 f"amounts to {found_person[2]} $. Last change amounts to {found_person[3]}$. YTD change is {found_person[4]} .          
 "\
 f"The country of origin is {found_person[5]} and the industry is {found_person[6]} .") 

except IndexError:
 print('Cannot find any rich people with given position-index!')

Breaking down the code:

  1. l is where we initialize the list (as you've mentioned in your question description)

  2. position - index of the person in the list. Basically index of a sublist in the initial list

  3. we have try - except condition in order to handle IndexError error. This error can appear if a sublist with a given index by the user is not found in the initial list

  4. found_person is found sublist which is related to the person

  5. in the print after found_person we are printing the final information about the person by simply addressing the index of every piece of information (every element in the found sublist). We can print them out by including these elements (ie found_person[1] , found_person[2] and so on) in curly brackets { } inside the string and putting f in front of it (just like in the question description)

In order to advise you. This is just an Example of what you looking for. I'm trying to make it simple and clear.

persons = [['Name', 'Age', 'hair Color'],
           ['Alice', '25', 'blonde'],
           ['Bob', '33', 'black'],
           ['Ann', '18', 'purple']]

position = int(input("Hello, this is list of 3 persons. "\
f"Press number to get information about it:"))

Name= persons[position][0] # get data from 1st column 
Age=  persons[position][1] # get data from 2nd column 
Color=persons[position][2] # get data from 3rd column 


print(f"The person with a position number {position} is {Name}. Has {Age} old."\
f" They have a {Color} hair color.") 

我不得不修改你的建议,因为我忘了提到我导入 .csv 并且搞砸了一些东西,但你们仍然非常帮助我,谢谢!

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