![](/img/trans.png)
[英]Creating a function that takes a DataFrame as an argument and returns a dictionary of scores
[英]Creating a function that takes scores as parameters that outputs to a print function
我要問的問題是 - 這個 function 將以球員列表和分數列表作為參數,並將 output 列表的內容顯示在屏幕上。 此 function 以“屏幕格式”部分下的分配規范中指定的格式將信息顯示到屏幕上。 您必須在解決方案中使用循環。
其中列表位於另一個文本文件 - player.txt 中,其內容是:
Ray Holt
15
Jessica Jones
0
Johnny Rose
10
Gina Linetti
6
Alexis Rose
1
Buster Bluth
3
其中必須包含一個列表 function 以便它可以像這樣顯示:
====================================
- Player Summary -
====================================
- Name Score -
------------------------------------
- Ray Holt 15 -
------------------------------------
- Jessica Jones 0 -
------------------------------------
- Johnny Rose 10 -
------------------------------------
- Gina Linetti 6 -
------------------------------------
- Alexis Rose 1 -
------------------------------------
- Buster Bluth 3 -
------------------------------------
====================================
代碼將按以下格式編寫:
# 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
不知道如何放置它,任何指針都會很棒!
編輯 - 不能使用內置函數,例如 len
如果兩個列表的長度相同,您可以像這樣對其進行迭代:
# 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]}")
內置zip(iterable1, iterable2)
的典型用例 - 當一個列表比另一個短時,它的優點是不會引發IndexError
。 要從等號和減號獲得水平線,您不需要寫整行 - 您可以在此處使用字符串乘法:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.