简体   繁体   English

没有按顺序调用函数?

[英]Functions not being called in order?

I'm writing a program using python3 to simplify a report I use for work. 我正在使用python3编写程序,以简化用于工作的报告。 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() . 它将调用enter_waste()但随后跳转到calculate_waste_percentage而不是运行enter_weight() Is there a way to fix this? 有没有办法解决这个问题?

You are missing input call in your input_weight() function: 您在input_weight()函数中缺少input调用:

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. 我建议还添加一个检查,以确保正确输入了计算机名称,以防止将来出现这种错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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