0

So i know its more basic stuff, but im trying to update a list outside a function.

Im making App where you ca n learn new words in diff types of games but basically the rest of the code is not important in this case.

Lets say i have a dict:

dict = {'jajko':'egg',
        'szynka':'ham',
        'mleko':'milk'      
       }

Now from that dict id like to make a list from which ill take index 0 and create another list with only 1 value so i dont have to make loop inside my app and pause it waiting for users answer.

Now wait for user to quess the value to the key. If you quess i remove index 0 from Result list and take next value as index 0 and so on untill list is empty.

Each time a player will press a Button inside the app he will run the function inside a class and play until list is empty.

Ive tried doing someting like this but im pretty sure im missing some basic stuff to make it happen. Everytime i run my func i remove value but when i start func again list is the same as it was on the beggining

dict= {'szynka':'ham','mleko':'milk','jajko':'egg'}
result = list(dict.items()).copy()

def lol():
    mylist = [result[0]]
    print(mylist[0][0])
    input1 = input('kk: ')
    if input1 != mylist[0][1]:
        print('wrong')
    elif input1 == mylist[0][1]:
        print('ok')
        del result[0]
        print(result)
        mylist = [result[0]]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    There's no need to use `.copy()` when assigning `result`. It's already a fresh list. – Barmar Aug 22 '23 at 17:41
  • Rather than convert to a list, I would probably just iterate over `items()` unless it was somehow important to actually delete the keys. – JonSG Aug 22 '23 at 17:44
  • When I run your function and type the correct answer it removes the item from the list. – Barmar Aug 22 '23 at 17:44
  • The last assignment to `mylist` is not needed, since the function ends there and the variable goes away. – Barmar Aug 22 '23 at 17:46
  • Why do you need `mylist` at all? Just use `result[0][0]` and `result[0][1]`. – Barmar Aug 22 '23 at 17:47
  • @Barmar yes it does inside but when i run func again it starts from 0. In my case everytime user will press button he will start that function so i want to update list outside function, so next time you press press button(start func) it will start with removed value already – Piotr Sygnatowicz Aug 22 '23 at 17:52
  • That's what it does. When I print `result` after the function, the item has been removed. – Barmar Aug 22 '23 at 17:54
  • There's no start button here. I suspect the code for the start button includes `result = list(dict.items()).copy()`, which is putting everything back. – Barmar Aug 22 '23 at 17:54
  • 1
    Please post a [mre] that demonstrates the problem. – Barmar Aug 22 '23 at 17:55

0 Answers0