简体   繁体   中英

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

when i have deined it as a function in the class where it all is written. 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

because you are using params even before you have declared it

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

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