上下文:我编写了一个程序,要求用户为用户定义的玩家数量输入姓名和分数(整数)。然后,程序将这些值格式化、切片并存储在名为golf.dat的文件中
问题:我正在尝试扩展我的程序,以允许它识别得分最低的球员,并打印得分最低的球员的姓名和得分
我的代码:
def playerDataInput(playerQuantity):
outputFile = open("golf.dat", "w")
for currentPlayer in range (1, playerQuantity + 1):
playerName = (input("Please enter player " + format(currentPlayer, "d",)+"'s name: "))
playerScore = (input("Please enter player " + format(currentPlayer, "d",)+"'s score: "))
outputFile.write(playerName + "\n")
outputFile.write(str(playerScore) + "\n")
outputFile.close()
def playerDataOutput():
inputFile = open("golf.dat", "r")
playerName = inputFile.readline()
while playerName != "":
playerScore = inputFile.readline()
playerName = playerName.rstrip("\n")
playerScore = playerScore.rstrip("\n")
print(playerName + ("'s score was:"), playerScore)
playerName = inputFile.readline()
inputFile.close()
def lowestScorer():
integers = open("golf.dat", "r")
lowestScore = 0
for score in integers:
if lowestScore >= int(score.strip()):
lowestScore = int(score.strip())
integers.close()
print("The lowest score was: ", lowestScore)
我尝试过:我尝试过编写一个函数(lowestScorer
)来提取和显示最小值,但一开始我没有想到,但根据我的数据存储方式,这显然会失败
您可以提供帮助的地方:建议是否有任何Pythonic方法来调整我的代码(而不是重写),以允许我的程序识别和显示保存在我的golf.dat
文件中的最低记分器/整数的名称和记分器
我的怀疑是,我应该用两个列表编写这个程序,分别保存名称和分数,然后将它们写入golf.dat(以便于提取和分析)或创建这两个列表(作为子列表,因此分别保存名称和整数分数值),但更进一步,并将它们保存在主列表中
目前没有回答
相关问题 更多 >
编程相关推荐