0

The following code references a dict (kanji_kana) of kanji/kana words saved in a different .py file.

kanji, kana = random.choice(list(kanji_kana.items()))

for i in kanji_kana:
    print(f"{kanji}, {kana}")
    print(f"type the kana for {kanji}.")
    answer = input("--> ")
    if answer == kana:
        print("right")
    else:
        print("wrong")

The output after two runs is as follows:

土地, とち
type the kana for 土地.
--> とち
right
土地, とち
type the kana for 土地.  ## the same key/value pair shown again
--> 

What I would like to do is after the user types the answer, whether right or wrong, is for the key/value pair to change to a different random pair from the dict. I want to cycle through the whole dict until all pairs are shown in the quiz.

kaisen
  • 1
  • 3
  • 1
    Shuffle the dict.keys() first, loop through the shuffled keys, pop used keys from shuffled list. Do until list empty. Repeat from start. – Patrick Artner Jun 18 '20 at 19:48
  • 1
    Well you're never updating `kanji` nor `kana` in the loop... Why are you even using `random` here? Why not just `for kanji, kana in kanji_kana.items():`? – Tomerikoo Jun 18 '20 at 19:48
  • Thanks for the replies! I'm using random because I want random pairs to be used in the quiz. When I tested it using print(f"{kanji}, {kana}"), it printed a different pair each time I ran the program. But in the for loop, it repeats the same pair. I'm a newbie, so I can't really try suggestions unless there is an example of code. I will try Tomerikoo's suggestion and see if that works. Thanks! – kaisen Jun 18 '20 at 20:46
  • @ Patrick Artner I tried to shuffle the dict.keys() but it threw a TypeError: 'dict_keys' object is not subscriptable – kaisen Jun 18 '20 at 21:29

1 Answers1

0

Use the dictionary keys attribute

keys = kanji_kana.keys()
for key in keys :
    print(kanji_kana[key])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • 3
    You can iterate directly on the dict: `for key in d:`, it will iterate on the keys, not necessary to call `keys()` – Tomerikoo Jun 18 '20 at 19:50
  • 1
    Don't use `dict` as a variable name since it shadows the builtin `dict`. I edited the answer to use OP's dict name to make it clearer anyway. – wjandrea Jun 18 '20 at 20:49
  • This is very helpful, thank you! This works if random is not needed. However, I need to have the pair be random. – kaisen Jun 18 '20 at 21:02
  • @kaisen funny enough, it could be pseudorandom on earlier versions of Python – wjandrea Jun 18 '20 at 21:06
  • So, my question was "associated" with another question marked [duplicate]. I looked at that question, which is very different from this one, and doesn't really help me at all. This is my first question ever, so I'm maybe doing it wrong. But my random pair is coming from a dictionary, not a list, and I'm trying to go through the whole dictionary using both key and value in my for loop. My problem is I don't know how to update or whatever it is I need to do to have a new pair each time. – kaisen Jun 18 '20 at 21:22
  • @kaisen You're not doing it wrong, don't worry, that's just how questions work on SO. Please [edit your question](https://stackoverflow.com/posts/62458113/edit) about the duplicate, then it'll be nominated for reopening. @-tag me in a comment and I'll put in a reopen vote myself. Now, the duplicate question will help you, but you need to convert your dict to a list somehow. I'll write you an answer to explain :) – wjandrea Jun 19 '20 at 16:33
  • Thank you for being awesome wjandrea! I edited the question, but I'm not sure if it's better or worse. – kaisen Jun 19 '20 at 21:15