0

I am trying to rewrite an old GUI-based adventure game of mine and am running into trouble. I am nothing but a humble beginner, so I can't properly diagnose what is going on here. In my code I use and if statement to see if they have the item skull in their inventory (inv). Even if inv==skull, the program continues down to the last else as if they don't have the skull in their inventory, instead of going to line 64 and seeing that inv==('skull').

Any and all help would be greatly appreciated, including those of how to optimize by code, not just fix it :)

Edit: I have began trying to use a list (Tuple) and then checking if the item is in the list, and it still isn't working :(


Edited Code:

inv=['skull']
def r_room():
        def witch_fight():
            def fgt_slap():
                rand = random.randint(1, 13)

                if rand <= 12:

                    slap_die()

                else:

                    slap_win()

            def fgt_sh():
                rand = random.randint(1, 13)

                if rand <= 9:

                    sh_die()

                else:

                    sh_win()

            def fgt_gc():
                rand = random.randint(1, 13)

                if rand <= 7:

                    gc_die()

                else:

                    gc_win()

            fgt_play()

            global inv
            if inv in ['skull', 'crest']:

                img_loc = ('resources\\images\\witch.gif')
                bg_img = tk.PhotoImage(file = img_loc)
                bg_lbl = tk.Label(image = bg_img)
                bg_lbl.image = bg_img
                bg_lbl.place(x=0, y=0)

                lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
                            fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
                lbl1.place(x=400, y=6)

                btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
                btn1.place(x=330, y=654)

                btn2 = tk.Button(text='Use Skeleton Head?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_sh()])
                btn2.place(x=640, y=654)

                btn3 = tk.Button(text='Use Golden Crest?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                command = lambda:[btn1.destroy(), btn2.destroy(), btn3.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_gc()])
                btn3.place(x=950, y=654)


            else:
                if inv in ['skull']:

                    img_loc = ('resources\\images\\witch.gif')
                    bg_img = tk.PhotoImage(file = img_loc)
                    bg_lbl = tk.Label(image = bg_img)
                    bg_lbl.image = bg_img
                    bg_lbl.place(x=0, y=0)

                    lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
                                fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
                    lbl1.place(x=400, y=6)

                    btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                    command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
                    btn1.place(x=330, y=654)

                    btn2 = tk.Button(text='Use Skeleton Head?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                    command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_sh()])
                    btn2.place(x=640, y=654)

                else:
                    if inv in ['crest']:

                        img_loc = ('resources\\images\\witch.gif')
                        bg_img = tk.PhotoImage(file = img_loc)
                        bg_lbl = tk.Label(image = bg_img)
                        bg_lbl.image = bg_img
                        bg_lbl.place(x=0, y=0)

                        lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
                                    fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
                        lbl1.place(x=400, y=6)

                        btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                        command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
                        btn1.place(x=330, y=654)

                        btn2 = tk.Button(text='Use Golden Crest?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                        command = lambda:[btn1.destroy(), btn2.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_gc()])
                        btn2.place(x=640, y=654)

                    else:

                        img_loc = ('resources\\images\\witch.gif')
                        bg_img = tk.PhotoImage(file = img_loc)
                        bg_lbl = tk.Label(image = bg_img)
                        bg_lbl.image = bg_img
                        bg_lbl.place(x=0, y=0)

                        lbl1 = tk.Label(text='Time to teach this witch who\'s boss!',
                                    fg='#cdd0d0', bg='#000000', font = ['Helvetica', 15])
                        lbl1.place(x=500, y=6)

                        btn1 = tk.Button(text='Slap Her?', font = ['Helvetica', 25], bg='#000000', fg='#cdd0d0', width=16,
                                        command = lambda:[btn1.destroy(), lbl1.destroy(), bg_lbl.destroy(), snd_stop(), fgt_slap()])
                        btn1.place(x=480, y=654)
TJF
  • 23
  • 4
  • Is the indentation correct? with functions 3 deep? – Reblochon Masque Jun 18 '18 at 08:33
  • Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Mike Scotty Jun 18 '18 at 08:33
  • Why did I pick this duplicate: You should use an iterable (set, list, tuple) for your inventory and check the contents with ``in``. And you should also restructure your ``rand==...`` checks. – Mike Scotty Jun 18 '18 at 08:35
  • @MikeScotty Thanks for the help, I did some research on tuples and it seemed to work but now I am encountering this error https://pastebin.com/6PRpS5dS – TJF Jun 18 '18 at 09:14

1 Answers1

0

A few tips: (1) I would strongly suggest mastering the basics before attempting to work with Tkinter. For example:

if rand==1 or rand==2 or rand==3 or rand==4 or rand==5 or rand==6 or rand==7 or rand==8 or rand==9 or rand==10 or rand==11 or rand==12:

Can be succinctly shortened to:

if rand <= 12:

(2) For your particular work: I would strongly suggest you learn about class objects and the hasattr() function. I really like this line that I read somewhere: "A class is more or less a fancy wrapper for a dict of attributes to objects." which should point you in the right direction with respect to checking if an item exists in an inventory.

James
  • 274
  • 4
  • 12