-2

Assigned all my global variables. College task is to input 2 numbers and add them together then if the total is greater than 100 but less than 200, then it print Drugs found.

global num_1
global num_2
global total_1

def input_num():
  num1 = int(input("Please your first number: "))
  num2 = int(input("Please your second number: "))
  total(num1, num2)

def total(num1, num2):#
  total_1 = num1 + num2
  procedure(total_1)

def procedure(total):
  total_1 = (int(total_1))
  if (total_1>100 and total_1<100):
      print ("Drugs found")

  else:
      print("No drugs found")

input_num()
total()
procedure()
martineau
  • 119,623
  • 25
  • 170
  • 301
Zak Islam
  • 1
  • 2

2 Answers2

1

You misunderstood how to use global variable:

  # when you define the variable in the module level no need for global keyword
  total_1 = None

  def procedure():
        # when you want to use a global variable declare it as global
        global total_1
        # here you can use it

This goes for all variables num1, num2

Here is your code when you use global variable:

num_1 = None
num_2 = None
total_1 = None

def input_num():
  global num_1
  global num_2
  num_1 = int(input("Please your first number: "))
  num_2 = int(input("Please your second number: "))

def total():#
  global total_1
  global num_1
  global num_2
  total_1 = num_1 + num_2

def procedure():
  global total_1
  # no need to convert to int is all ready an int
  if 100 > total_1 > 200:
      print ("Drugs found")

  else:
      print("No drugs found")

input_num()
total()
procedure()

Another solution with comments to help you understand what is happening:

def input_num():
    """ convert user input to int and return a tuple."""
    num1 = int(input("Please your first number: "))
    num2 = int(input("Please your second number: "))
    return (num1, num2)


def total(num1, num2):
    """ accept two number return the sum."""
    return num1 + num2


def procedure(total):
    """ check the total and print a message."""
    if 100 > total> 200:
        print ("Drugs found")
    else:
        print("No drugs found")


# this called tuple unpacking
# input_num return a tuple we unpack it in num_1, and num_2
num_1, num_2 = input_num()
# when you define a function that accept two arguments you should pass
# this argument to it when you call it, and save the returned value
total_1 = total(num_1, num_2)
# pass the total to
procedure(total_1)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
0

The function will look inside the local scope first to find total_1, but total_1 is a global variable and it needs to be specified while you write the function:

def procedure(total):
  global total_1
  total_1 = (int(total_1))
  if (total_1>100 and total_1<100):
      print ("Drugs found")
  else:
      print("No drugs found")

I did a good explanation about the same in this answer : Why is a global dictionary accessible inside a class whereas a global integer variable is not?

pissall
  • 7,109
  • 2
  • 25
  • 45