2
Paremeter passing
tdudek (3)

Hi,
I'm messing around with parameter passing in functions. The total is not being correctly passed here. What is the problem?

You are viewing a single comment. View All
Answered by heyitsmarcus (280) [earned 5 cycles]
View Answer
2
heyitsmarcus (280)

@tdudek When you're passing variables between modules, what you want to do is return values from that module back to the main script so that they can be used. In this case, you don't need the global keyword to store the values. main.py needs to be changed slightly and input_module.py needs to be changed slightly. main.py will call input_module and then get those values back from the input_module's returned values.

Here's a repl I made from your code: https://repl.it/@heyitsmarcus/Module-Example-3-1

main.py

#Developed on 28 June 2019

#importing the modules
import input_module
import product_module

#Main Program
#Setting all variables to zero
#total=0
num1=0
num2=0


#Calling the modules
#Only change here is that num1, num2 get the values back from the called module
num1, num2 = input_module.InputNums()
product_module.CalculateProduct(num1, num2)

input_module.py

def InputNums():
  #remove all parameters and use global num1, num2 to store value
  #this way num1 and num2 can be used in the next function
  #global num1,num2
  num1=int(input("Enter the first number: "))
  num2=int(input("Enter the second number: "))
  #And the module now returns those values
  return num1, num2

Cheers!