Ex36 correct args variable


#1

So I have been trying to set up my ex36 game. One idea I had as an exercise was to use the modules to import different “toolkits” that could be unpacked during the game and based on which you pick at the beginning would determine the outcome of some of the rooms. I have been playing around and finally got the open_toolkit function to work. What I am now running into is that the script opens the same args variable which results in the same tool kit being opened. What I tried to set up was an if-statement that depending on what bag the person said they brought that would change the selection of argv variable that was used as the item running through the open_toolkit function (see snake_room()). I have played around with a few options, changing the defining of the variables, changing the variable names, but how can I get it to call the other toolkit. Each toolkit has different items listed in it.

from sys import argv

script, toolkit_1, toolkit_2, finish = argv

def open_toolkit(toolbag):

    stuff = open(toolbag).read()
    supplies = stuff.split(', ')
    print(f"Looks like you have a {supplies[0]} and a {supplies[1]}.")

def snake_room():
    print("""After opening up the door to the first room you realize it
is full of snakes.
The door is on the other side of the room to get out. Which supply bag did
you grab? \nBag 1 or Bag 2?""")
    tool = input("> ")
    if "Bag 1" or "1" in tool:
        toolbag = toolkit_1
    elif "Bag 2" or "2" in tool:
        toolbag = toolkit_2
    else:
        print("You did not pick correctly. Let's try again")
        snake_room()

    open_toolkit(toolbag)

    print("""What do you think you can use from that supply bag to get past
the snakes?""")
    kill_snakes = input("> ")

    if "shovel" in kill_snakes:
        print("Great job! You shovelled all the snakes outta here!")
        room_b()
    elif "childrens book" in kill_snakes:
        print("You start to read the chilrens book.")
        print("Some of the snakes fall asleep and you are able to get by.")
        print("""You are able to get to the closest door in the room.
Hopefully this is the right way to get through the castle......""")
        room_a()
    elif "flashlight" in kill_snakes or "spider hammer" in kill_snakes:
        print("Too bad for you, this supply kit did not help you here.")
        print("You quickly run out of the room and try a different way out.")
        start()
    else:
        dead("Your inability cripples you and the snakes attack!")

#2

I edited your post and put [code] ... [/code] around your code to make it readable.

Try setting toolbag to something that “fails” before doing the if-statement. A common pattern is to do this:

toolbag = 'no choice'
tool = input("> ")
if '1' in tool:
    toolbag = toolkit_1
elif '2' in tool:
    toolbag = toolkit_2
else:
    snake_room()

assert toolbag != 'no choice', f"Failed to choose: {toolbag}."

The assert is a way to say, “assert that this doesn’t happen.” and your program will bomb and print out what toolbad actually was. In this case you know the assert will explode when toolbag is equal to ‘no choice’, meaning your above else: is not working as expected.

I’ll also point out a common thing people who speak English do when first programming:

If I say “If my dog or my cat is sick I’ll get some medicine.” This works in English because we can figure out you mean if my dog is sick OR my cat is sick, but it’s actually unclear. What you actually wrote is that you will get medicine if dog. End. If you have a dog you will get medicine. Then if your cat is sick, you will get medicine.

A computer does not suffer this lack of clarity lightly. You have to say everything exactly, so when you say:

if 'Bag 1' or '1' in toolkit:

You’re actually saying this:

if 'Bag 1' is True or '1' in toolkit:

In Python, ‘Bag 2’ is always true, so that will always trigger that if-statement. You have to rewrite it:

if 'Bag 1' in toolkit or '1' in toolkit:

Then it will work, maybe. Usually if you make this mistake in one place you make it in many others so check all your if-statements for this flaw.