LouisLove795
LouisLove795

Reputation: 3

python passing parameter modules

I cant seem to get this, I'm just not understanding how to pass parameters between modules. Granted this seems really simple to me but maybe I'm just not getting it, I am very new to python but do have programming experience.

def main():

  weight = input("Enter package weight: ")

  return weight

def CalcAndDisplayShipping(weight):

  UNDER_SIX = 1.1
  TWO_TO_SIX = 2.2
  SIX_TO_TEN = 3.7
  OVER_TEN = 3.8

  shipping = 0.0

  if weight > 10:
    shipping = weight * OVER_TEN
  elif weight > 6:
    shipping = weight * SIX_TO_TEN
  elif weight > 2:
    shipping = weight * TWO_TO_SIX
  else:
    shipping = weight * UNDER_SIX

  print ("Shipping Charge: $", shipping)


main(CalcAndDisplayShipping)

When I run this I get: Enter Package weight: (num)TypeError: unorderable types: function() > int()

Could anyone explain this to me?

Upvotes: 0

Views: 81

Answers (3)

Sijeesh
Sijeesh

Reputation: 337

def CalcAndDisplayShipping(weight):

    UNDER_SIX = 1.1
    TWO_TO_SIX = 2.2
    SIX_TO_TEN = 3.7
    OVER_TEN = 3.8

    shipping = 0.0

    if weight > 10:
        shipping = weight * OVER_TEN
    elif weight > 6:
        shipping = weight * SIX_TO_TEN
    elif weight > 2:
       shipping = weight * TWO_TO_SIX
    else:
       shipping = weight * UNDER_SIX

    print ("Shipping Charge: $", shipping)

if __name__ == '__main__':

    weight = float(input("Enter package weight: "))
    CalcAndDisplayShipping(weight)

If you are running this script using python interpreter like python script_name.py, __name__ variable value will be '__main__'.

If you are importing this module to some other modules __name__ will not be __main__ and it won't execute the main section.

So you can use this feature if you want to do anything while you are running this module as an individual script.

This 'if condition' only satisfies if you are running your module as an individual script.

Upvotes: 0

mhawke
mhawke

Reputation: 87074

I think that you mean this:

CalcAndDisplayShipping(main())

This will call main() and pass its return value as an argument to CalcAndDisplayShipping().

Upvotes: 0

smushi
smushi

Reputation: 719

One thing is in python there is no need for a main. One other way to do it, that does the job is this.

Do you really need a main?

import os


def CalcAndDisplayShipping(weight):

   UNDER_SIX = 1.1
   TWO_TO_SIX = 2.2
   SIX_TO_TEN = 3.7
   OVER_TEN = 3.8

   shipping = 0.0

   if weight > 10:
      shipping = weight * OVER_TEN
   elif weight > 6:
      shipping = weight * SIX_TO_TEN
   elif weight > 2:
      shipping = weight * TWO_TO_SIX
   else:
      shipping = weight * UNDER_SIX

   print ("Shipping Charge: $", shipping)

weight = float(input("Enter package weight: "))

CalcAndDisplayShipping(weight)

Upvotes: 1

Related Questions