简体   繁体   中英

Functions not being called in order?

I'm writing a program using python3 to simplify a report I use for work. It's not supposed to be architecturally sound or elegant, really I just bruted my way through it just to the point of it "working".

The methods by themselves do work fine, but when I enter them, they don't get called in order when I run them. I'm going to post the entire code here, because I know someone is going to ask something related to what's not show. Again, I wrote this really quickly just to work. It's not supposed to be anything built by a professional.

My question is, why are my methods not called in order. I do it this way so that the dictionary values are filled in by the input and then it's supposed to calculate the precentages. But what happens is, it calls the calculate percentage method out of order, so I'm stuck getting a division by Zero error (which yes is true when called out of order because the info needed technically hasn't been entered yet).

def enter_waste():
    while True:
        machine_selection = str(input('Please enter which machine you would like to enter the waste for: '))
        for element in machine_list:
            if machine_selection == element['name']:
                print('You Have Selected: {}'.format(element['name']))
                waste_input = int(input('Please enter the waste for this period: '))
                element['waste'].append(waste_input)
                if str(input('Do you want to continue? yes/no: ')) == 'no':
                    print("\nHere's everything as entered: \n")
                    for i, machine in enumerate(machine_list):
                        print(i, machine)
                return False

def enter_weight():
    machine_selection = str('Please enter which machine you would like to enter the produced weight for: ')
    for element in machine_list:
        if machine_selection == element['name']:
            print('You have selected: {}'.format(element['name']))
            weight_input = int(input('Please enter weight produced for this period: '))
            element['weight'] = weight_input
            if str(input('Do you want to continue? yes/no: ')) == 'no':
                print("\nHere's everything as entered: \n")
                for i, machine in enumerate(machine_list):
                    print(i, machine)
                return False

def calculate_waste_percentage():
    for element in machine_list:
        percentage = (sum(element['waste'])/element['weight']) * 100
        element['percentwaste'] = percentage
        print('Machine: {}\nWeight Produced: {;,}\n% of Waste: {}%'.format(element['name'], element['weight'], percentage ))
        return percentage

However, when I call them in order like this:

enter_waste(), enter_weight(), calculate_waste_percentage()

It will call enter_waste() but then jump to calculate_waste_percentage instead of running enter_weight() . Is there a way to fix this?

You are missing input call in your input_weight() function:

machine_selection = str(input('...'))

instead of

machine_selection = str('...')

As written, your code tries to set weight on a machine named "Please enter which machine you would like to enter the produced weight for: ", which does not exist.

I would suggest to also add a check that the machine name was entered correctly, to prevent this kind of error in the future.

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