1

我目前正在从 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

基本上,我的问题是如何在两个漂亮的列中显示球员的姓名和分数。我对输入代码或输出代码做错了吗?

我查看了一堆处理格式化列的答案,一切都比我的目的复杂。这是计算机编程课程的介绍,所以我需要一个简单的修复!

4

4 回答 4

0

请加

print player + "   " + score + "\n"

这将解决您的第一个问题。

如果您想在字符串中进行更多格式化,那么 String 具有格式化功能

字符串格式化操作

您正在以简单格式存储数据。如果您使用 CSV,那么它将很容易阅读。

于 2013-10-25T06:24:07.510 回答
0

首先,写入文件的代码应该是

# loop to get names and scores, both of which are saved in array
counter = 0
while counter < numPlayers:
    # gets the players' names
    player = raw_input("Please enter the player's name.")
    # gets the players' scores
    score = input("Please enter that player's score.")

    # Write the scores to the file in a comma-separated format
    outGolf.write(player+','+score+'\n')
    counter = counter + 1

然后,当你想做出漂亮的展示时

# 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
    allRecords = inGolf.readlines()
    # print the data
    for record in allRecords:
        player = record.split(',')[0]
        score = record.split(',')[1]

        print player + "\t" + score + "\n"

    # closes the file    
inGolf.close()

# calls main function
main()
于 2013-10-25T06:34:28.957 回答
0

你可以尝试这样的事情:

def main():
    inGolf = open('golf.txt', 'r')
    names = [] # to store names
    scores = [] # to store scores
    for line in inGolf: # reads file line by line
        line_list = line.split(",") # list formed by each word (separated by comma) 
        names.append(line_list[0]) # append to respective list
        scores.append(line_list[1])

    for i in range(len(names)): # printing
        print "{0:20}{1:10}".format(names[i], scores[i]) # 20 and 10 are the field length

    inGolf.close()

def w(numPlayers): # to write the file
    counter = 0
    outGolf = open('golf.txt', 'w')
    while counter < numPlayers:
        name = raw_input("Please enter the player's name:")
        outGolf.write(name + ",") # separate name and score by a comma
        score = input("Please enter that player's score:")
        outGolf.write(str(score) + "\n") # just add a "\n" to write in different line next time
        counter = counter + 1
    outGolf.close()

w(2)
main()
于 2013-10-25T06:35:23.973 回答
0

The file.read() method with no arguments reads the file till the EOF(end of file), so in your examle the score will be empty string and player == bob1bill2joe3will4mike5. And it's better to add end of line character('\n') so that you could iterate over content of your file.

于 2013-10-25T06:46:35.653 回答