1

i'm getting a SyntaxError on line 8 of the code below.

import random
import sys
a=random.randint(1, 30)
b=random.randint(1, 30)
c= "minus" or "plus"
print(c)
def f():
    if global b>global a:
      a=random.randint(1, 30)
      b=random.randint(1, 30)
    print(a+b) 
Jakubik
  • 11
  • 1
  • `c = "minus" or "plus"` does exactly the same thing as `c = "minus"`. I'm not sure what you think the `or "plus"` is doing. – chepner Oct 15 '21 at 16:22

1 Answers1

0

I am not going to discuss in great length the unnecessary use of global variables here (it would be better to pass a/b as parameters).

The syntax you used (if global b>global a:) is invalid. The global x is a definition of the scope of a variable, it is done once and should be on its own line.

The correct syntax should be:

import random
import sys
a=random.randint(1, 30)
b=random.randint(1, 30)
c= "minus" or "plus"             ## not sure what this is doing
print(c)
def f():
    global a                     ## definition of global scope
    global b
    if b>a:                      ## use of variables
      a=random.randint(1, 30)
      b=random.randint(1, 30)
    print(a+b) 

and presumably you want to call f at some point:

f()
mozway
  • 194,879
  • 13
  • 39
  • 75
  • that totally fixed it, thank you! the code is a bit messy, as i am new to coding, and it is a bit of a much longer program. – Jakubik Oct 15 '21 at 18:52