2016-11-17 73 views
-2

我修复了大部分代码,但唯一的问题是我没有显示任何文本。我应该输入高尔夫球员的名字和他们的分数,但是当我运行这个程序时什么都没有显示出来。运行我的代码并没有显示任何内容

def main(): 
    inGolf = open('golfers.txt', 'r') 
    names = [] 
    scores = [] 
    for line in inGolf: 
     line_list = line.split(",") 
     names.append(line_list[0]) 
     scores.append(line_list[1]) 

    for i in range(len(names)): 
     print ("{0:20}{1:10}".format(names[i], scores[i])) 
    inGolf.close() 

def w(numPlayers): 
    counter = 0 
    outGolf = open('playerData.txt', 'w') 
    while counter < numPlayers: 
     name = raw_input("Please enter the player's name:") 
     outGolf.write(name + ",") 
     score = input("Please enter that player's score:") 
     outGolf.write(str(score) + "\n") 
     counter = counter + 1 
    outGolf.close() 

main() 
+1

有一个整体的功能有你永远不打电话,也许看个明白? – jonrsharpe

+0

添加一些打印语句来检查你是否在'main()'中输入了第一个'for'循环' – mitoRibo

+1

并且当你处理它的时候,可能找出为什么文件名是不同的? –

回答

0

我稍微修改此脚本来尝试here和它实际工作:

  1. 它通过玩家提示次数球员的名字和分数。
  2. 它可以保存球员文件
  3. 它读取球员文件并显示得分结果。

我不得不改变raw_inputinput为Python3并称为w功能通过玩家的数量由用户输入:

def main(): 

    num_players = input("How many players?") 
    w(int(num_players)) 
    inGolf = open('golfers.txt', 'r') 
    names = [] 
    scores = [] 
    for line in inGolf: 
     line_list = line.split(",") 
     names.append(line_list[0]) 
     scores.append(line_list[1]) 

    for i in range(len(names)): 
     print ("{0:20}{1:10}".format(names[i], scores[i])) 
    inGolf.close() 


def w(numPlayers): 
    counter = 0 
    outGolf = open('golfers.txt', 'w') 
    while counter < numPlayers: 
     name = input("Please enter the player's name:") 
     outGolf.write(name + ",") 
     score = input("Please enter that player's score:") 
     outGolf.write(str(score) + "\n") 
     counter = counter + 1 
    outGolf.close() 

main() 
+0

谢谢你真是太棒了!也即时只是想弄清楚我将如何实施像“插入高尔夫球手的数量” – vtecjustkickedinyo

相关问题