0

I am trying to mak9e a love calculator, but it is throwing error that, score_total is not defined, I tried many things but it just doesn't works. I try to define it globally.

When I defined it globally as score_total=0. It just doesn't return from function, it keeps its value 0 outside the function. I am a beginner.

boy = input("Enter boy Name\n").casefold()
girl = input("Enter girl name\n").casefold()
#score_total=
vowel_a =0
vowel_b=0
boyl=None
girll=None
boyl=len(boy)
girll=len(girl)
#print(boyl)
cons_a =0
cons_b=0
for i in range(boyl):
    if boy[i] is 'a' or 'e' or 'i' or 'o' or 'u':
        vowel_a+=1
for i in range(girll):
    if girl[i] is 'a' or 'e' or 'i' or 'o' or 'u':
        vowel_b+=1

for i in range(len(boy)):
    if boy[i] is 'z' or 'b' or 't' or 'h':
               cons_a +=1
for i in range(len(girl)):
    if girl [i] is 'z' or 'b' or 't' or 'h':
               cons_b+=1
def calculation(a,b,score_total):
    score_total = 1
    if len(a) == len(b):
        score_total += 20
    elif len(a)!=len(b):
        score_total += 7.4
    if a[0] and b[0] is 'a' or 'b' or 'c' or 'd' or 'e':
        score_total +=10
    else:
        score_total+=3.7
    if a[0] and [b] is 'z' or 'b' or 't' or 'h':
        score_total+=5
    else:
        score_total +=1.16
    if vowel_a == vowel_b:
        score_total +=12
    else:
        score_total +=3.67
    if cons_a == cons_b:
               score_total+=12
    else:
               score_total+=4.56
    if a[0] is 'i' or 'v' or 'o' or 'l':
               score_total+=7
    else:
               score_total+=5.6
    #return int(score_total)
print(score_total)
#print(scor--
calculation(boy,girl,1)               
if score_total < 50:
    if score_total%2 ==0:
               score_total+=30
    elif score_total>100:
               temp = score_total-100
               score_total -= temp
    else:
               score_total+=24
import time
print("Calculating Result...........\n")
time.sleep(3)
print("Data processing...")
time.sleep(7)      
print(f'The love between {boy} and {girl} is {score_total}%' )
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

If you want to use a global variable (probably not the best design practice, but hey, I don't judge), you'll need to put "global score_total" at the top of any function that uses it. Ex:

def calculation(a,b,score_total):
  global score_total
  score_total = 1
  if len(a) == len(b):
  ....

Variables in functions in python are in a separate scope from the main program, and the global keyword overrides that.

  • Note, there are a good few other errors in your code. Things like: "if a[0] and b[0] is 'a' or 'b' or 'c' or 'd' or 'e'" are not going to evaluate the way you think they are. I just addressed the main question in the post. – SainTfactor Sep 08 '18 at 16:09
0

You are printing variable score_total before you call the calculation function, where score_total is defined. The calculation function should also return score_total to the caller, where you should assign the returning value to score_total.

At the end of the calculation function:

return int(score_total)

Then at the caller:

score_total = calculation(boy,girl,1)
print(score_total)
blhsing
  • 91,368
  • 6
  • 71
  • 106