运行我的代码,什么都没有显示

时间:2016-11-17 23:04:34

标签: python

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

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 个答案:

答案 0 :(得分:0)

我稍微修改了这个脚本以试用它here它实际上有效:

  1. 按球员号码提示球员的名字和分数。
  2. 保存播放器文件
  3. 它会读取球员档案并显示得分结果。
  4. 我必须将raw_input更改为input和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()