Function not running when called

The generate_responce function is not running when called.

Instead of running it quits the program

I want it to run when called on lines 414, 423, 430, and 435.
The function is on line 252

https://replit.com/@JacobHooker/semi-open-ended-npc-encounter#main.py

You are calling the generate_response without passing the player_input argument. You need to pass a string to it since the function itself requires one argument. So, when you call generate_response, pass the player_input as an argument. For example, change the line npc.generate_response(player_input) to npc.generate_response("some input") or ensure player_input has a value before the function call.

Also, if there’s an error within the generate_response function, it can cause the program to exit out of nowhere, that’s why you add error handling to know what’s going on. Like, add some print statements before and inside the generate_response function to check if the function is being reached and what input it’s receiving.

4 Likes

I have player_input defined as an input statement earlier in the code, should I still define it within the parentheses? It is defined on lines 393 and 422. Because of this, would it be an error within the function that would result in it closing the program?

I saw that you changed the code, do you still have the problem?

If you still have it try to wrap your main loop in a try-except block and see where the error is poping

An example:

def main():
    print("Welcome to the RPG Market Adventure!")
    print("What is your name, adventurer?")
    player_name = input()
    player = Player()
    player.name = player_name
    open_ended_npc = OpenEndedNPC(random.choice(npc_names))

    while True:
        try:
            print(f"\n{player.name}, you find yourself in {random.choice(places)}.\n")
            print("What would you like to do?")
            print("1. Visit the Market")
            print("2. Talk to a random NPC")
            print("3. Take on a quest")
            print("4. Quit")

            choice = input()

            if choice == "1":
                display_market(player, open_ended_npc)
            elif choice == "2":
                display_npc_greeting(open_ended_npc)
            elif choice == "3":
                generate_random_quest(player)
            elif choice == "4":
                print("Thank you for playing! See you next time.")
                sys.exit()
            else:
                print("Invalid choice. Please enter 1, 2, 3, or 4.")
        except Exception as e:
            print(f"An unexpected error occurred: {e}")
2 Likes

I resolved the issue, i took your advice and added a print statement to see if the function was running. It was. From there I looked through my code and it turns out I was calling the wrong function. Just calling the generate_responce function wasnt going to do anything because it had nowhere to return its response to and, I assume, therefore exited the program. What I should have been doing is calling the interact function which gives the generate_responce function somewhere to output its responce. Thank you for your assistance!!

1 Like

What is the try-except block for? Wouldn’t the error propagate anyway?

1 Like

To identify and catch the error?

But why would you need to catch it? It’ll still print the error in the console even if you don’t add the print statement.

1 Like

I think I’m not making myself clear, with the try-except you can handle the error in a controlled way.

Sometimes you might want to handle different exceptions differently. So, with a try-except block, you can tailor to catch specific exceptions and handle them in a specific way.

Heck, you can even provide a user-friendly message, so I don’t see why not use a try-except

1 Like

Ive seen the try-exept block used before but I don’t really understand how to use it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.