我目前正在做一个问题,从编程逻辑和设计开始,第三版:
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
基本上,我的问题是如何在两个漂亮的列中显示球员的名字和得分。输入代码或输出代码有问题吗?在
我看了一大堆关于格式化列的答案,一切都比我的目的复杂。这是一个计算机编程入门课,所以一个简单的修复是我需要的!在
你可以试试这样的方法:
首先,写入文件的代码应该是
然后,当你想做一个漂亮的展示
^{pr2}$请补充
这将解决你的第一个问题。在
如果您想在字符串中进行更多的格式化,那么string具有格式化功能
String Formatting Operations
您正在以简单格式存储数据。如果你使用CSV,那么它将很容易阅读。在
相关问题 更多 >
编程相关推荐