Fenomatik
Fenomatik

Reputation: 477

passing arguments to function in the imported module from console

I have a simple script

import os
os.system('cls')
def addition (a , b ):
    """
    Learning Python to make a better world
    This is my first program
    """
    c = a+b
    print (c)

addition(3,8)

How can I pass the arguments to the function addition from the console after its imported ?

I am sure this is very basic question but somehow I am struggling with this.

Upvotes: 1

Views: 130

Answers (2)

An Ting Shi
An Ting Shi

Reputation: 36

Modified the addition function as class structure:

Class Add:    
  def __init__(a,b):
    self.a = a
    self.b = b

  def addition (self):
    """
    Learning Python to make a better world
    This is my first program
    """
    c = self.a+slef.b
    print (c)

 if __name__ == '__main__':
   add1 = Add(sys.argv[1:])
   Add.addition()

Upvotes: 0

Jacob Ritchie
Jacob Ritchie

Reputation: 1401

If you use sys instead of os, you can do

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
addition(a,b)

where you would call the script using a command like

python addition.py 1 2

where addition.py is the name of your script.

Upvotes: 2

Related Questions