5

First off, i have to tell you that i am completely new to coding, so the problem i have might be caused by the most stupid misstake ever, and if so, im sorry!

I am trying to make a calculator which is able to calculate +,-,*,/. It's also supposed to give an error message and ask for a new operation symbol if the one recieved was invalid. To let the computer know if the funtion "main" is being run because it recieved an invalid funtion, or if it is first time its being run, i am trying to use a global variable called "check". At the start, check is set to 1, and so the computer will use the first phrase when asking for an operation. If an invalid operation is entered, the variable "check" is incresed by one, which will lead to the second phrase (the error message) when it asks for a new operation.

The problem is that when i try to run the script, i get a syntax error on the first line, where "global check = 1". What am i doing wrong?

Below is my code:

global check = 1

#returns num1 + num2
def add(num1,num2):
    return num1 + num2

#returns num1 - num2
def sub(num1,num2):
    return num1 - num2

#returns num1 * num2
def mul (num1,num2):
    return num1 * num2

#returns num1 / num2
def div (num1,num2):
    return num1 / num2

#Main Function
def main():
    if(global check == 1): #checks if "main" has been read before, if it has, then it is read agian because of invalid operation, and the global "check" should be higher than 1.
        operation = input("Choose an operation! (+,-,*,/")
    else:
        operation = input("You must choose a valid operation! (+,-,*,/")
    if(operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        global check = global check + 1
        main()
    else:
        var1 = int(input("Enter number 1 :"))
        var2 = int(input("Enter number 2 :"))
    if(operation == "+"):
        print(add(var1,var2))
    elif(operation == "-"):
        print(sub(var1,var2))
    elif(operation == "*"):
        print(mul(var1,var2))
    else:
        print(div(var1,var2))


main()
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Isac Skoglund
  • 69
  • 1
  • 1
  • 2
  • I Dont understand why the code is shown in such a way, i didnt type it like that but it still is put like that... – Isac Skoglund Feb 05 '17 at 18:55
  • 1
    It's because SO uses markdown. Fixed now. Looks to me there is no reason to use `global`: here `check` is already global... – Willem Van Onsem Feb 05 '17 at 18:56
  • And when you've fixed that, there are several other places where you wrongly place `global`. It's a keyword, you can't use it in expressions. – Daniel Roseman Feb 05 '17 at 19:03
  • Thanks for the relpy, it fixed that error, but now im getting it at line 21, in the if statement. Could you please explain where i should use global and where i shouldnt, aswell as why? – Isac Skoglund Feb 05 '17 at 19:26
  • Possible duplicate of [Python function global variables?](http://stackoverflow.com/questions/10588317/python-function-global-variables) – Grisha Levit Feb 05 '17 at 19:28

3 Answers3

7

You are putting global where it's unneeded:

global check = 1

You don't need global here, check is already global here.

if(global check == 1), global check = global check + 1 is also not a valid use of global.

Instead, declare check as global in main():

def main():
    global check
Reaper
  • 747
  • 1
  • 5
  • 15
  • Reaper, Thanks for the help, that solved it, but now i get a syntax error in the if statement: File "calculator.py", line 21 if(global check == 1): ^ SyntaxError: invalid syntax – Isac Skoglund Feb 05 '17 at 19:17
  • @Isac Skoglund You only need to use `global check` once in a function, then just use `check` like `if(check == 1):` or `check += 1`. – Reaper Feb 05 '17 at 19:21
1

Use like this

check=10

def function() : 

     global check

     if(check==1):

           #blablabla

The global keyword helps you to bring value to your function... Then it's useless and unnecessary to use that in your function again...

For simplicity, global helps you to bring your friend to your home, and then there is no need for you to bring him again since he is already here...

Ahmad
  • 1,618
  • 5
  • 24
  • 46
-1

Any variable of same name, defined outside of functions and inside a function are by default global and local respectively. If a global variable value needs to be changed inside a function or a local variable needs to be made global(available for all other functions), then only it is required to define 'global' explicitly inside the function.

example:

a=10


def func1():
global b
global a
print('initial global a',a)
b=10
a=15
print('local and modified global a',a)


def func2():
print('global b, originated form func1',b)
print('new global a, modified at func1',a)



func1()
func2()

will give you result:

initial global a 10
local and modified global a 15
global b, originated form func1 10
new global a, modified at func1 15

Note: If there is no conflict of local and global variable name inside a function it is not required to declare global inside a function

  • Welcome to StackOverflow! The SO community tries to collect curated, high-quality answers, to be shared with everyone. When responding to such old questions, you should make sure that your answer actually addresses the original question, is properly formatted, and provides new value. Some insights might be better suited as comments as well. The code you shared does not compile, and doesn't seem to directly provide new input. You could try to condensate the main idea into a comment instead. – hyperTrashPanda Jul 04 '19 at 08:05