我目前正在从 Programming Logic & Design, Third Edition 开始做一个问题:
Springfork 业余高尔夫俱乐部每个周末都会举办一场锦标赛。俱乐部主席要求你设计两个项目。(1) 一个程序,它将读取每个球员的姓名和高尔夫分数作为键盘输入,然后将这些作为记录保存在名为 golf.dat 的文件中。(每条记录都会有一个玩家姓名字段和一个玩家得分字段。) (2) 从 golf.dat 文件中读取记录并显示它们的程序。
我已经创建了第一个没问题。我遇到的第二个问题。这是我的代码:
# main module/function
def main():
# opens the "golf.txt" file created in the Golf Player Input python
# in read-only mode
inGolf = open('golf.txt', 'r')
# reads the player array from the file
player = inGolf.read()
# reads the score array from the file
score = inGolf.read()
# prints the names and scores
print player + " " + score
# closes the file
inGolf.close()
# calls main function
main()
我无法显示球员姓名和分数。我拥有它的方式如下所示:
bob1bill2joe3will4mike5
我的第一个程序中的列表代码是这样的:
# loop to get names and scores, both of which are saved in array
counter = 0
while counter < numPlayers:
# gets the players' names
player[counter] = raw_input("Please enter the player's name.")
# writes the names to the file
outGolf.write(player[counter] )
# gets the players' scores
score[counter] = input("Please enter that player's score.")
# writes the scores to the file
outGolf.write(str(score[counter]) )
counter = counter + 1
基本上,我的问题是如何在两个漂亮的列中显示球员的姓名和分数。我对输入代码或输出代码做错了吗?
我查看了一堆处理格式化列的答案,一切都比我的目的复杂。这是计算机编程课程的介绍,所以我需要一个简单的修复!