简体   繁体   中英

Creating a number of functions that need to be able to call each other in Python

So first let me say that I am a novice at Python and functions seem to be out of my comprehension for the moment but where I am having trouble is having 3 functions be able to call each other. Here is my code(yes I know it is terribly wrong but you should see where I am going):

def menu():
    count=gearboxes
    cost=subtotal
    return subtotal


def quantity():

    gearboxes=raw_input("How many gearboxes would you like to order? ")

    return menu()



def subtotal(cost):
    if (gearboxes<=10):
        cost=gearboxes*100.0
        print cost
    elif (gearboxes>10 and gearboxes<20):
        cost=(gearboxes-10)*80.0+1000.0
        print cost
    elif (gearboxes>20):
        cost=(gearboxes-20)*70.0+1000.0+800.0
        print cost
    else:
        print "wtf m8"

    return menu()

def summary():
    print "="*80
    print "%60s %20f %20f" % ("motors",count,cost)
    print "="*80


print quantity()
print subtotal(menu)
print summary(menu)

There is it and any help would be greatly appreciated if you could explain also kind of how functions call on each other.

Thanks!

fixed version(still working)

    def quantity():
        motors=raw_input("How many motors would you like to order? ")
        gearboxes=raw_input("How many gearboxes would you like to order? ")
        sensors=raw_input("How many sensor boards would you like to order? ")

        return int(motors),int(gearboxes),int(sensors)



   def subtotal(motors,gearboxes,sensors):

        if motors<=10 and gearboxes<=15:
            motorCost=motors*100
            gearboxCost=gearboxes*50
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

        if motors>10 and motors<=20 and gearboxes>15 and gearboxes<=30:
            motorCost=(motors-10)*80+1000
            gearboxCost=(gearboxes-15)*40+750
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

        elif motors>20 and gearboxes>30:
            motorCost=(motors-20)*70+1000+800
            gearboxCost=(gearboxes-30)*30+750+600
            sensorCost=sensors*66
            return motorCost, gearboxCost, sensorCost

    def summary(motors,gearboxes,sensors,motorCost,gearboxCost,sensorCost):
        print "="*80
        print "%60s %20d %20d" % ("motors",motors,motorCost)
        print "%60s %20d %20d" % ("gearboxes",gearboxes,gearboxCost)
        print "%60s %20d %20d" % ("sensor boards",sensors,sensorCost)
        print "="*80


   def menu():

        a,b,c=quantity()
        d,e,f=subtotal(a,b,c)
        summary(a,b,c,d,e,f)
        return


    menu()

I made some changes to your code. Treat a function like a question. When you call the function; you're asking the question. What you pass to return is the answer to the question. So when someone asks for the subtotal of some number of gearboxes; we return cost , whatever that may be.

We can then store the return values (the answers) in variables and use them later. For example, to pass to another function. Try to follow how information flows through the program.

def quantity():
    count=raw_input("How many gearboxes would you like to order? ")
    return int(count)



def subtotal(count):
    if count<=10:
        cost=count*100.0
        return cost
    elif count>10 and count<20:
        cost=(count-10)*80.0+1000.0
        return cost
    elif count>20:
        cost=(count-20)*70.0+1000.0+800.0
        return cost

def summary(count, cost):
    print "="*80
    print "%60s %20f %20f" % ("motors",count,cost)
    print "="*80

def menu():
    items = quantity()
    sub = subtotal(items)
    summary(items, sub)

if __name__ == '__main__':
    menu()

"subtotal" already calls menu() so I'm not sure what you are asking since you are already calling one function within the other.

Also, I can't see what your program is supposed to do - if your function names would be verbs (print_menu, get_menu, set_menu, throw_menu_on_moon, calculate_subtotal, ...) it would be better to understand for humans.

Also, the names you use (on the right hand side of =) within a function must be known there, so for example

def menu():
    count=gearboxes

makes no sense (because "gearboxes" is unknown - on the other hand, "count" is fine since it defines new variable - since it is on the left hand side of =)...

Note that variables are only known within the function you defined them in, so

def f():
    gearboxes = 2
def menu():
    count=gearboxes

would make no sense either.

But

def f():
    return 2
def menu():
    gearboxes=f()
    count=gearboxes

would make perfect sense.

Read the

def calculate_subtotal(gearbox_count):

as "to calculate subtotal of gearbox count do".

If you then say anywhere outside:

calculate_subtotal(5)
^^^^^^^^^^^^^^^^^^^^^

then the underlined part will be replaced by the result returned.

Otherwise, in Python the lines (in a block) are executed one after another - if you want to do multiple things in sequence, you can just write them one line each, one after another.

"return" is not "goto", "return" gives back the result - and control - to the caller of the function. Then the result is placed into the program "instead of the call".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM