Hi I'm working on a Python programming prompt and I am running into a bug. Here is the code prompt I'm trying to solve:

You are given a string of words and numbers. Extract the expression including:

the operator: either addition ("gains") or subtraction ("loses")
the two numbers that we are operating on
Return the result of the calculation.

Notes:
"loses" and "gains" are the only two words describing operators
No fruit debts nor bitten apples = The numbers are integers and no negatives

Here is my code that has a bug in it:

def calculate(string):
    num = 0
    for word in string.split():
        if word == 'loses':
            operator = '-'
        elif word == 'gains':
            operator = '+'
        elif word.isdigit():
            num += int(word)
    return operator + str(num)
system
hai main ek paythan programing prompt par kaam kar rahaa hum or main ek bag main chal rahaa hum.yahan code prompt he jise main hal karne kii koshish kar rahaa hum:

aapako shabdon or sankhyaon kaa ek taar diya jaataa he.sahit abhivyakti nikaalen:

oparator: yaa to iske alaava ("laabh") yaa ghataav ("haar")
jin do numberon par ham kaam kar rahe hai
gananaa kaa parinaam lautaaen.

tippaniyan:
"haar" or "laabh" keval do shabd hai jo operataron kaa varnan karte hai
koi fal rin nahin or na hi kaate hue seb = sankhyaayein purnank hai or koi nakaaratmak nahin he

yahan meraa code he jisamen ek bag he:

DEF gananaa (string):
    sankhya = 0
    string.split () main shabd ke liye:
        yadi shabd == 'haar':
            oparator = '-'
        eliff shabd == 'laabh':
            oparator = '+'
        ELIF WORD.ISDIGIT ():
            sankhya += int (shabd)
    vaapasi oparator + STR (sankhya)
user
aisa lagata he ki jis tarah se aap unhen string se nikaalane ke baad ankon ko sangrahit kar rahe hai, vah galat drushtikon he.

DEF gananaa (string):
    sankhya = [] # sankhyaon ko sangrahit karne ke liye ek suchi kaa upyog karen
    string.split () main shabd ke liye:
        yadi shabd == 'haar':
            oparator = '-'
        eliff shabd == 'laabh':
            oparator = '+'
        ELIF WORD.ISDIGIT ():
            num.append (shabd) #Append ko jodne ke bajaay
    ritern EVAL (STR (NUM [0]) + oparator + STR (NUM [1])) #suchi kaa upyog karne ke liye ritern statement

1.) char sankhya ko ek suchi main badalen.yah aapako vaakya se donon number store karne kii anumati detaa he.
2.) ab ise sankhya main jodne ke bajaay sankhya suchi main maan len
3.) ant main, aapke dwaara paae gaye oparator or sankhya suchi main sangrahit do sankhyaon kaa upyog karke abhivyakti kaa moolyankan karen
assistant
<|system|>
</s>
<|user|>
hai main ek paythan programing prompt par kaam kar rahaa hum or main ek bag main chal rahaa hum.yahan code prompt he jise main hal karne kii koshish kar rahaa hum:

aapako shabdon or sankhyaon kaa ek taar diya jaataa he.sahit abhivyakti nikaalen:

oparator: yaa to iske alaava ("laabh") yaa ghataav ("haar")
jin do numberon par ham kaam kar rahe hai
gananaa kaa parinaam lautaaen.

tippaniyan:
"haar" or "laabh" keval do shabd hai jo operataron kaa varnan karte hai
koi fal rin nahin or na hi kaate hue seb = sankhyaayein purnank hai or koi nakaaratmak nahin he

yahan meraa code he jisamen ek bag he:

DEF gananaa (string):
    sankhya = 0
    string.split () main shabd ke liye:
        yadi shabd == 'haar':
            oparator = '-'
        eliff shabd == 'laabh':
            oparator = '+'
        ELIF WORD.ISDIGIT ():
            sankhya += int (shabd)
    vaapasi oparator + STR (sankhya)</s>
<|assistant|>
aisa lagata he ki jis tarah se aap unhen string se nikaalane ke baad ankon ko sangrahit kar rahe hai, vah galat drushtikon he.

DEF gananaa (string):
    sankhya = [] # sankhyaon ko sangrahit karne ke liye ek suchi kaa upyog karen
    string.split () main shabd ke liye:
        yadi shabd == 'haar':
            oparator = '-'
        eliff shabd == 'laabh':
            oparator = '+'
        ELIF WORD.ISDIGIT ():
            num.append (shabd) #Append ko jodne ke bajaay
    ritern EVAL (STR (NUM [0]) + oparator + STR (NUM [1])) #suchi kaa upyog karne ke liye ritern statement

1.) char sankhya ko ek suchi main badalen.yah aapako vaakya se donon number store karne kii anumati detaa he.
2.) ab ise sankhya main jodne ke bajaay sankhya suchi main maan len
3.) ant main, aapke dwaara paae gaye oparator or sankhya suchi main sangrahit do sankhyaon kaa upyog karke abhivyakti kaa moolyankan karen</s>
