简体   繁体   中英

Creating a function that takes scores as parameters that outputs to a print function

The question I was asking was - This function will take the list of players and the list of scores as parameters and will output the contents of the lists to the screen. This function displays the information to the screen in the format specified in the assignment specifications under the section - 'Screen Format'. You must use a loop in your solution.

In which the list is in another text file - players.txt which contents are:

Ray Holt
15
Jessica Jones
0
Johnny Rose
10
Gina Linetti
6
Alexis Rose
1
Buster Bluth
3

which must include a list function so it can be displayed like this:

====================================
- Player Summary -
====================================
- Name Score -
------------------------------------
- Ray Holt 15 -
------------------------------------
- Jessica Jones 0 -
------------------------------------
- Johnny Rose 10 -
------------------------------------
- Gina Linetti 6 -
------------------------------------
- Alexis Rose 1 -
------------------------------------
- Buster Bluth 3 -
------------------------------------
====================================

Code is to be written in this format:

# Function display_players() - place your own comments here...  : )
def display_players(player_list, score_list):

    # This line will eventually be removed - used for development purposes only.
    print("In function display_players()")

    # Place your code here

Not sure how to place it in, any pointers would be great!

Edit - Cant use inbuilt functions such as len

If both lists have the same length, you can iterate over it like this:

# Function display_players() - place your own comments here...  : )
def display_players(player_list, score_list):
    print('- Player Summary -')
    for i in len(player_list):
        print(f"- {player_list[i]} {score_list[i]}")

Typical use-case for the builtin zip(iterable1, iterable2) - it has the advantage of not raising an IndexError when one of the lists is shorter than the other. To get the horizontal lines from equal and minus signs, you don't need to write the whole line - you can use string multiplication here:

def display_players(player_list, score_list):
    print("=" * 36)
    print("- Player Summary -")
    print("=" * 36)
    count = 0
    for player in player_list:
        score = score_list[count]
        print(f"- {player} {score} -")
        print("-" * 36)
        count += 1
    print("=" * 36)

def test():
    with open("players.txt", "r") as f:
        lines = f.readlines()
    
    players = []
    scores = []
    
    for line in lines:
        line = line.strip() # remove trailing and leading spaces
        if line: # if the string line is not empty …
            if line.isdigit(): # … and the string consists only of numbers …
                scores.append(int(line)) # convert it to int and append it to scores …
            else: # if there are alphabetical letters in the string, append it to players
                players.append(line)
    display_players(players, scores)

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