简体   繁体   English

文本文件中的Python保龄球程序字典

[英]Python bowling program dictionary from text file

Below is my assignment. 以下是我的作业。 I am stuck on how to include the total in the dictionary. 我对如何将总计包括在字典中感到困惑。 I am not even sure if this is possible but I need it to do the average. 我什至不确定这是否可行,但我需要用它来做平均。 I would appreciate a push in the right direction. 我希望朝正确的方向前进。 :) :)

Assignment:Write a program that will read an unknown number of bowlers and their bowling scores (with possible values from 1 to 300) from an external file called "bowlingscores.txt". 作业:编写一个程序,该程序将从名为“ bowlingscores.txt”的外部文件中读取未知数量的保龄球及其保龄球得分(可能的值是1到300)。 The file will look similar to the following: 该文件将类似于以下内容:

David
102
Hector
300
Mary
195
Jane
160
Sam
210

Output the bowlers' names to an external data file called "bowlingaverages.txt". 将常礼帽的名称输出到名为“ bowlingaverages.txt”的外部数据文件。 Next to each bowler's name, print a message dependent on their scores: For perfect scores (equal to 300), output “perfect” For those scores greater than the average score, output “above average” For those below average, output “below average” 在每个保龄球运动员的名字旁边,打印一条取决于其得分的消息:对于完美得分(等于300),输出“完美”对于那些得分高于平均得分的得分,输出“高于平均水平”对于那些低于平均得分的得分,输出“低于平均得分” ”

scores = {}  
total = 0


def bowl_info(filename):
    infile = open("bowlingscores.txt", "r")

    for line in infile:    
        if line.strip().isdigit():
            score = int(line)
            scores[name] = score
            total += score    
        else:
            name = line.strip()
    return  scores




bowl_info("bowlingscores.txt")
numbowlers = len(scores)
total = 0
average = total / numbowlers

Would it not be possible to simply add the total as a key in the dictionary and update it in the loop as you have done? 难道不能简单地将总数作为关键字添加到字典中,然后像您所做的那样在循环中更新它?

scores = {'total': 0}  


def bowl_info(filename):
    infile = open("bowlingscores.txt", "r")

    for line in infile:    
        if line.strip().isdigit():
            score = int(line)
            scores[name] = score
            scores['total'] += score    
        else:
            name = line.strip()
    return  scores




bowl_info("bowlingscores.txt")
numbowlers = len(scores)
#total = 0 REMOVE THIS LINE
average = scores['total'] / numbowlers

Return score as well total , 回报score以及total

def bowl_info(filename):
    total = 0 # you have to define toatl within function.
    .. 
    ..
    return  scores, total

Catch both object through function call and use it in your code:- 通过函数调用捕获两个对象,并在您的代码中使用它:

scores, total = bowl_info("bowlingscores.txt")

#score = {'Jane': 160, 'Hector': 300, 'Mary': 195, 'Sam': 210, 'David': 102}
#total = 967

check and analyze it, i covered all the you want: 检查并分析它,我涵盖了所有您想要的:

>>> my_dict ={}
>>> f = open('bowlingscores.txt')
>>> for x in f:
...     my_dict[x.strip()] = int(f.next())   # f.next() moves the file pointer to nextline and return is value
... 
>>> my_dict
{'Jane': 160, 'Hector': 300, 'Mary': 195, 'Sam': 210, 'David': 102}
>>> total_score = sum(my_dict.values())   
>>> total_score
967
>>>avg = float(total_score/len(my_dict.values()))
193.0
>>> for x,y in my_dict.items():
...     if y == 300:
...         print x,y,"Perfect"
...     elif y >=avg:
...         print x,y,"Above Average"
...     elif y <= avg:
...         print x,y,"Below Average"
... 
Jane 160 Below Average
Hector 300 Perfect
Mary 195 Above Average
Sam 210 Above Average
David 102 Below Average

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM