简体   繁体   English

如何正确定义 Python 中的 function ?

[英]How to define a function in Python properly?

Well I was writing this simulation by following a tutorial and at the end, I got this in the error (those are some variables):好吧,我正在按照教程编写此模拟,最后,我在错误中得到了这个(这些是一些变量):

num_cashiers, num_servers, num_ushers = get_user_input() NameError: name 'get_user_input' is not defined num_cashiers, num_servers, num_ushers = get_user_input() NameError: name 'get_user_input' 未定义

when i have deined it as a function in the class where it all is written.当我在 class 中将其定义为 function 时,所有内容都写入其中。 Help!帮助!

Here's some code:这是一些代码:

        def get_user_input():
        num_cashiers = input("Enter no. of cashiers working: ")
        num_servers = input("Enter no. of servers working: ")
        num_ushers = input("Enter no. of ushers working: ")

        # Now, brace yourself! Do try to understand this stuff

        if all(str(i).isdigit() for i in params): # Check if input is valid
            params = [int(x) for x in params]
        else:
            print("Could not parse input. The simulation will use default values: ",
                  "\n1 cashier, 1 server, 1 usher")

            params = [1, 1, 1]
        return params

    def main():
        # Setup
        random.seed(42)
        num_cashiers, num_servers, num_ushers = get_user_input() 

        # Run the Simulation
        env = simpy.Environment()
        env.process(run_theater(env, num_cashiers, num_servers, num_ushers))
        env.run(until=90)

        # View the results
        mins, secs = get_average_wait_time(wait_times)
        print(
            "Running simulation...",
            f"\nThe average wait time is {mins} minutes and {secs} seconds.",
            )

    if __name__ == '__main__':
        main()

Actually there is an indentation error in your script.实际上,您的脚本中存在缩进错误。 Correct one正确一个

def get_user_input():
    num_cashiers = input("Enter no. of cashiers working: ")
    num_servers = input("Enter no. of servers working: ")
    num_ushers = input("Enter no. of ushers working: ")
       .......

Your indentation isn't correct.你的缩进不正确。

def get_user_input():
  num_cashiers = input("Enter no. of cashiers working: ")
  num_servers = input("Enter no. of servers working: ")
  num_ushers = input("Enter no. of ushers working: ")

  # Now, brace yourself! Do try to understand this stuff

  if all(str(i).isdigit() for i in params): # Check if input is valid
      params = [int(x) for x in params]
  else:
      print("Could not parse input. The simulation will use default values: ",
            "\n1 cashier, 1 server, 1 usher")

      params = [1, 1, 1]
  return params

def main():
  # Setup
  random.seed(42)
  num_cashiers, num_servers, num_ushers = get_user_input() 

  # Run the Simulation
  env = simpy.Environment()
  env.process(run_theater(env, num_cashiers, num_servers, num_ushers))
  env.run(until=90)

  # View the results
  mins, secs = get_average_wait_time(wait_times)
  print(
      "Running simulation...",
      f"\nThe average wait time is {mins} minutes and {secs} seconds.",
      )

if __name__ == '__main__':
    main()

I would also like to remind you that if you run this code, you will get an error saying我还想提醒您,如果您运行此代码,您将收到一条错误消息

UnboundLocalError: local variable 'params' referenced before assignment UnboundLocalError:分配前引用的局部变量“params”

because you are using params even before you have declared it因为您甚至在声明它之前就使用了params

ok, you should first assign params then check if user inputs are correct or not like this:好的,您应该首先分配参数,然后检查用户输入是否正确,如下所示:

def get_user_input():
        num_cashiers = input("Enter no. of cashiers working: ")
        num_servers = input("Enter no. of servers working: ")
        num_ushers = input("Enter no. of ushers working: ")

        params = [num_cashiers,num_servers,  num_ushers] # <------ I've added this line

        if all(str(i).isdigit() for i in params):
            params = [int(x) for x in params]
        else:
            print("Could not parse input. The simulation will use default values: ",
                  "\n1 cashier, 1 server, 1 usher")

            params = [1, 1, 1]
        return params

Of course, your indentation isn't correct.当然,你的缩进是不正确的。 I hope you know that我希望你知道

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

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