代码Python 3的麻烦

时间:2015-08-27 09:37:54

标签: python arrays sorting arraylist

我是python的新手,我还在学习,我无法在我的阵列中尝试这个:

  • 每个赛事的获胜者
  • 拥有最佳平均值的房子
  • 获胜次数最多的房子

使用此代码:

获取事件名称:

Flag = True
while Flag:
    try:
        e = str(input("Enter Event names [Type XXX to stop]: "))
        if e == 'XXX' or e == 'xxx':
            Flag = False
        else:
            event.append(e)
    except ValueError:
        print("Please enter a word, Thanks")
print()

获得房屋名称:

Flag = True
while Flag:
    try:
        h = str(input("Eneter House names [Type XXX to stop]: "))
        if h == 'XXX' or h == 'xxx':
            Flag = False
        else:
            total = total + 1
            house.append(h)
    except ValueError:
        print("Please enter a word, Thanks")
print()

获取事件和房屋的点数

for i in (event):
    for j in (house):
        Flag = True
        while Flag:
            try:
                sevent = int(input("Enter %s's house for %s score: "%(j, i)))
                if sevent < 0:
                    print("Enter a number above 0, Thanks")
                else:
                    Flag = False
            except ValueError:
                print("Please enter a number above, Thanks")

        if j not in scores:
            scores[j] = []
        scores[j].append(sevent)
        score.append(sevent)

我已经得到了打印总冠军和每个赛事的总得分,但我不知道如何获得每个赛事的冠军,平均成绩最好的房子以及获胜次数最多的房子。 / p>

获得每项赛事的总冠军和总分:

for house, score in sorted(scores.items(), key=lambda x: sum(x[1]),reverse=True):
    print ("House %s's Total Score: %i"%(house, sum(score)))
print(scores)
print("The Overall Winner is", max(scores, key=lambda h: sum(scores[h])))
print()

输出的一个例子是:

House House 1's Total Score: 80House House 3's Total Score: 70
House House 2's Total Score: 40
The Overall Winner is House 1

示例的数组如下:

{'House 1': [20, 60], 'House 2': [30, 10], 'House 3': [40, 30]}

感谢您的帮助,我真的很感激。

0 个答案:

没有答案