需要帮助,尝试修复 python 中的错误

问题描述 投票:0回答:1

我编写了一个程序来模拟团队之间的比赛并打印分数和分数,乍一看,它似乎可以工作,但每隔一段时间,也许运行后四次中有一次,它不会添加分数并且我似乎不知道是什么导致了这个问题

我尝试的任何方法似乎都无法修复该错误。代码:

import random

#A list of the parameters for the teams and their actions
action=['teamplay1','yellow','red','teamplay2']
teamplay1=['score','defence','almost','offside','var']
teamplay2=['score','defence','almost','offside','var']
teams=['Chelsea','Arsenal','Barcelona','Madrid']

#Setting the intial value of the score and point to zero
global score1,score2
score1=0
score2=0
point1=0
point2=0

#selecting the teams and making sure that it doesnt select the same team twice
a=random.choice(teams)
teams.remove(a)
b=random.choice(teams)

   
#defining the choices for the action list and printing relevant text based on the random choice
def action_choices():
 global score1,score2
 x=random.choice(action)
 if x =='teamplay1':
    y=random.choice(teamplay1)
    if y == 'score':
        print(a + ' scores')
        #To add the goal to the score of the team
        score1=score1+1

    elif y == 'defence':
        print('Good defence by ' + a + ', they broke down the ' + b + ' attack')

    elif y == 'almost':
        print(a +  ' almost scored')

    elif y == 'offside':
        print(a +  ' are offside')

    elif y == 'var':
        print(a + "'s goal is ruled out by offside")

    else:
        print('ERROR')

 elif x =='teamplay2':
    y=random.choice(teamplay2)
    if y == 'score':
        print(b + ' scores')
        #To add the goal to the score of the team
        score2=score2+1

    elif y == 'defence':
        print('Good defence by ' + b + ', they broke down the ' + a + ' attack')

    elif y == 'almost':
        print(b +  ' almost scored')

    elif y == 'offside':
        print(b +  ' are offside')

    elif y == 'var':
        print(b + "'s goal is ruled out by offside")

    else:
        print('ERROR')

 elif x=='yellow':
    z=random.choice(teams)
    print('Yellow card for '+ z)

 elif x=='red':
    z=random.choice(teams)
    print('Red card for '+ z)

#defining the clock for the match and printing the scores of both teams    
def process(clock=0):
    print('Minute ' + str(clock) + ' : ')
    action_choices()
    clock=clock+10
    if clock==90:
        print('Match is over.')
        print(a + ' : ' + str(score1) + ' ' + b + ' : ' + str(score2))
    else:
        process(clock)

#creating a function to calculate the points for the teams based on whether they won, drew or lost and printing them out
def add_point():
 global point1, point2
 if score1>score2:
    point1=point1 + 3

 elif score1==score2:
    point1=point1 + 1
    point2=point2 + 1

 elif point2>point1:
    point2=point2 + 3

 else:
    print('Error')
 print(a + ' points : ' + str(point1) + ' ' + b + ' points : ' + str(point2))

#playing the match
def play():
    print("Today's match is between " + a + " and " + b)
    process()
    add_point()
    
    
play()

python random
1个回答
1
投票

您的代码的问题似乎出在 add_point() 函数中。问题是您正在检查 point2 是否大于 point1,如果是,则将 point2 增加 3。但是,您并不是首先检查 point2 是否大于 point1。这个条件永远不会成立,因为 point2 和 point1 都初始化为 0,并且在调用 add_point() 函数之前不会更改。 这是更正后的 add_point() 函数:

def add_point():
    global point1, point2
if score1 > score2:
    point1 = point1 + 3
elif score1 == score2:
    point1 = point1 + 1
    point2 = point2 + 1
elif score2 > score1:
    point2 = point2 + 3
else:
    print('Error')
print(a + ' points : ' + str(point1) + ' ' + b + ' points : ' + str(point2))
© www.soinside.com 2019 - 2024. All rights reserved.