用于受控测试的Python分数计数器

时间:2014-10-12 23:55:16

标签: python

我在大约七个小时内进行了一次受控评估,要求我建立一个可以保持比赛得分的计划。在本次比赛中有4支球队和5支比赛。我需要一个程序,在平局时给予1分,主场胜利2分,客场胜利3分。当输入错误的团队编号时,我还需要它来显示错误消息。有人可以帮忙吗?它也需要在python中。

我只编了几个星期而且我不知道我在做什么。我试图找出如何在输入错误的团队编号时打印出一个语句而不在循环中计算

到目前为止我的代码:

schoolnumber = [1,2,3,4]
homescores =[0,0,0,0]
awayscores=[0,0,0,0]

for counter in range(0, 5):
    whohost = int(input("Who hosted the game? "))
    whowins = int(input("Who was the winner? "))
    if whohost == whowins:
        homescores[whowins-1] += 2
    else:
        awayscores[whowins-1] += 3

print(homescores)
print(awayscores)

2 个答案:

答案 0 :(得分:0)

for counter in range(0, 5):
    while True:
        try:
            whohost = int(input("Who hosted the game? "))
            whowins = int(input("Who was the winner? "))
            if whohost in schoolnumber and whowins in schoolnumber:
                break
            else:
                raise ValueError()
        except ValueError:
            print('Please enter a valid number from the list {}'.format(schoolnumber)) 
    if whohost == whowins:
        homescores[whowins-1] += 2
    else:
        awayscores[whowins-1] += 3

这将循环显示两个input语句,直到输入的数字都在schoolnumber中。如果输入了不在列表中的数字或字符串,它将显示错误消息并重新开始。

答案 1 :(得分:-1)

好的代码应该是这样的(我不知道它是什么运动,希望我没有犯错)

MAX_SCHOOLS = 4
MAX_GAMES = 5

teams = [0]*MAX_SCHOOLS

for i in range(MAX_GAMES):
    host = int(input("Who hosted the game [1 to {}]? ".format(MAX_SCHOOLS)))
    winner = int(input("Who was the winner (0 for tie) ? "))
    away = int(input("Away team [1 to {}] ? ".format(MAX_SCHOOLS)))
    if winner == away:
        print("Error : Away team = Host team")
        continue # skip this game
    if not 0 <= winner < MAX_SCHOOLS:
        print("Error : winner incorrect")
        continue
    if not 0 <= host < MAX_SCHOOLS:
        print("Error : host incorrect")
        continue
    if not 0 <= away < MAX_SCHOOLS:
        print("Error : winner incorrect")
        continue
    if host == winner:
        teams[host-1] += 2
    elif away == winner:
        teams[away-1] += 3
    else:
        teams[away-1] += 1
        teams[host-1] += 1

print(teams)