Best way to access lists and nest them with if statements Trying to improve my Python skills by creating a small text adventure. I have used lists to access certain information, but I am not sure if I am using them correctly. For instance, I have used a list to store weapons but wasn't sure of the syntax for accessing them in an if statement. Also, if there is any advice on how to improve the code in general that would be appreciated. I am a beginner so would prefer a simple alternative.
weapon = [&quot;sword&quot;, &quot;stone&quot;, &quot;flower&quot;]  


# Create user defined functions for the game first.

def title():
    print(&quot;In front of you there is a portal. You have no idea where it leads&quot;)
    print(&quot;portal #1&quot;)

# Call function

title()
portal = input(&quot;Type 1: &quot;)

if portal == &quot;1&quot;:
    print(&quot;There's a wizard conjuring a spell.&quot;)
    print(&quot;What do you do?&quot;)
    print(&quot;1. Talk to the wizard.&quot;)
    print(&quot;2. Attack the wizard.&quot;)
    print(&quot;3. Run away.&quot;)
    
    choice_wizard = input(&quot;Type 1,2,3: &quot;)
    
    if choice_wizard == &quot;1&quot;:
        print(&quot;The wizard turns you into a frog. Ribbit!&quot;)

    elif choice_wizard == &quot;2&quot;:
        print(&quot;You see a sword laying on the ground&quot;)
        print(&quot;Would you like to take the sword? Press y or n to continue&quot;)
    
        weapon_input = input(&quot;Type y or n: &quot;)
        if weapon_input == &quot;y&quot; or weapon_input == &quot;Y&quot;:
            weapon = []
            weapon.append(&quot;sword&quot;)
            print(&quot;You are now carrying a &quot; + weapon[0] + &quot;, You kill the wizard&quot;)
        elif weapon_input == &quot;n&quot; or weapon_input == &quot;N&quot;:
            weapon = []
            weapon.append(&quot;stone&quot;)
            print(&quot;You are now carrying a &quot; + weapon[0] + &quot;, You hit the wizard in the head with it&quot;)
            print(&quot;He falls over unconscious and dies&quot;)           
    elif choice_wizard == &quot;3&quot;:
        print(&quot;The wizard tries turning you into a frog but misses.&quot;)
        print(&quot;...You escape.&quot;)
Here are just a few comments to help improve your code:

When you have a collection, think about how you are going to access the collection. Most of the time, you are going to treat it as a group of items (example: &quot;weapons&quot;) and so you should use a plural name. Rarely, you will treat a collection as a single item (example: &quot;database&quot;), and so you should use a singular name.

Always use functions. Yes, python supports having statements directly &quot;up against the wall.&quot; But it is much, much easier to write test cases, to debug your code, and to change your mind about what order to do things if you have a single name (function) for a group of statements. Moving things around as blobs of text is miserable and prone to errors.

When you find yourself doing something twice, write a function! In your case, asking the user for some input is a good place to put a function:
 show_message(&quot;There's a wizard conjuring a spell.&quot;)
 choice = get_choice(&quot;What do you do?&quot;,
       &quot;Talk to the wizard&quot;,
       &quot;Attack the wizard&quot;,
       &quot;Run away&quot;)


As far as your collection of weapons is concerned, think about what kind of game you are writing. If you are writing a &quot;dungeon&quot; style game, where there is going to be a lot of fighting monsters, then having real weapon-objects with stats will be important. In that case, you'll probably want either a set or a list, but you'll want a separate variable to track which of the possibly many weapons is currently being wielded.
On the other hand, if you are writing more of a &quot;text adventure&quot; game, where just having &quot;the sword&quot; is enough to beat a monster, then you might not want a collection at all. Instead, you might want one or more boolean variables, or perhaps a dictionary full of booleans.


      if user_has['the sword']:
          show_message(&quot;The troll runs away!&quot;)
      else:
          show_message(&quot;The troll eats you. You have died.&quot;)


To specifically answer your question about how to access members of lists: note that you are using the list.append() function. This takes an object and adds it to the end of the list. So if you have 11 items in the list, and you .append another item, it will be the 12th item (index 11, because list indexes are 0-based).
You can access that item in two ways. The &quot;hard way&quot; is to compute the length of the list and subtract one:
 item = &quot;sword&quot;
 weapons.append(item)
 sword_index = len(weapons) - 1
 print(&quot;You have a&quot;, weapons[sword_index])

An easier way is to reply on the fact that python supports negative indexes as being from the end of the list. That is, list[-1] is the last item, list[-2] is the next-to-last item, etc. This works with indexes and with slices, so you can also say list[1:-3] or list[-5:] for example. See note 3 in the sequence docs for details.