0

I'm writing a printer calculator program. I want to take a "parent sheet" divide it into "run sheets" and then the runs into finish sheets. The size of parent, run and finish are input by the user.

There are other variables to be added later, like gutter and paper grain, but for now I just want to divide rectangles by rectangles. Here's my first crack:

import math

def run_number(p_width, p_height, r_width, r_height):
    numb_across = p_width/r_width
    numb_up = p_height/r_height

    run_numb = numb_up * numb_across

    print math.trunc(run_numb)

def print_number(f_width, f_height, r_width, r_height):
    numb_across = r_width/f_width
    numb_up = r_height/f_height

    finish_numb = numb_up * numb_across

    print math.trunc(finish_numb)

#Parent size
p_width = input("Parent Width: ")
p_height = input("Parent Height: ")
#Run size
r_width = input("Run Width: ")
r_height = input("Run Height: ")
#finish size
f_width = input("Finish Width: ")
f_height = input("Finish Height: ")

run_number(p_width, p_height, r_width, r_height)

print_number(f_width, f_height, r_width, r_height)

Am I using the arguments correctly? It seems wrong to create variables, pass those variables as arguments, then use those argument values. But, how I would I do this better?

The input call is outside the functions, but I'm using the same variable name (p_width, p_height, etc.), as I'm using inside the function. I don't think this is breaking this program, but I can't believe it's good practice.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
icicleking
  • 1,029
  • 14
  • 38
  • 4
    *" It seems wrong to create variables, pass those variables as arguments, then use those argument values. "* - I don't see why you would think it is wrong. – Stephen C Sep 28 '14 at 01:43
  • Since you're defining your functions before declaring your module-level variables, it's very clear to the reader that any reference inside of a function's text refers to its argument, not to the module-level scope. This is fine. – Charles Duffy Sep 28 '14 at 19:35

2 Answers2

0

It is perfectly fine to pass variable to a function as arguments! (I don't think there is another way)

If you don't want to save space, you could use input without making it an extra variable like this:

Instead of:

a = input("A: ")
b = input("B: ")
someFunction(a,b)

You can use:

someFunction(input("A: "),input("B: "))
Electron
  • 308
  • 4
  • 13
0

I believe it is better than using global variables at all. Your method becomes much more general and independent of the context.

JSBach
  • 447
  • 1
  • 6
  • 13