使用从Python中的文件读取的列表创建列

2024-10-01 09:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个问题,从编程逻辑和设计开始,第三版:

The Springfork Amateur Golf Club has a tournament every weekend. The club president has asked you to design two programs. (1) A program that will read each player’s name and golf score as keyboard input, and then save these as records in a file named golf.dat. (Each record will have a field for the player’s name and a field for the player’s score.) (2) A program that reads the records from the golf.dat file and displays them.

我已经创造了第一个没问题。第二个我遇到麻烦了。这是我的代码:

    # 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()

我在显示球员姓名和分数时遇到了问题。我的方式是这样的:

^{pr2}$

我在第一个程序中的列表代码是:

        # 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

基本上,我的问题是如何在两个漂亮的列中显示球员的名字和得分。输入代码或输出代码有问题吗?在

我看了一大堆关于格式化列的答案,一切都比我的目的复杂。这是一个计算机编程入门课,所以一个简单的修复是我需要的!在


Tags: andtheto代码inreadnamesmain
3条回答

你可以试试这样的方法:

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()

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

# 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

然后,当你想做一个漂亮的展示

^{pr2}$

请补充

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

这将解决你的第一个问题。在

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

String Formatting Operations

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

相关问题 更多 >