I cannot get where my error is in my code.
JavaScript
x
22
22
1
def cauculator (num_1,num_2):
2
#num_1 is the first number to be cauculated, when num_2 is the second.
3
return (a+add+b+sub+c+mul+d+div)
4
5
div = num_1/num_2
6
mul = num_1*num_2
7
add = num_1+num_2
8
sub = num_1-num_2
9
10
#the reason I did this is because I will use these "subsitute" names to print the result out.
11
12
a= "Added"
13
b= "Subtracted"
14
c= "Multiplied"
15
d= "Divided"
16
17
print (a+add+str('n')+b+sub+str('n')+c+mul+str('n')+d+div)
18
19
print (cauculator) (3,8)
20
print (cauculator) (5,2)
21
print (cauculator) (9,5)
22
When I try to run this, the NameError
happens.
I do not know where my error is.
Advertisement
Answer
variables num1
and num2
are defined within the scope of function cauculator
but you are accessing them from the global scope where they are not defined. Your code also has some other issues too. here is an edited version of your code:
JavaScript
1
14
14
1
def cauculator (num_1,num_2):
2
#num_1 is the first number to be cauculated, when num_2 is the second.
3
div = num_1 / num_2
4
mul = num_1 * num_2
5
add = num_1 + num_2
6
sub = num_1 - num_2
7
output_string = f'Added = {add}, Subtracted = {sub}, Multiplied = {mul}, Divided = {div}'
8
return output_string
9
10
11
print(cauculator(3, 8))
12
print(cauculator(5, 2))
13
print(cauculator(9, 5))
14
feel free to ask if need help understanding it.