![](/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.