-1

I am completely new to python. I have been creating a vocabulary program, but I want the to mix the words, so also the ones behind :. So far it keeps throwing only the ones in front of :. How can I achieve this?

print()
print('Welcome to german vocabulary quiz!')


import random

answer = input('Ready?  ')
¨
print('')

while answer=='y' or 'yes':
    vocabDictionary={
        'e Arbeit':'pracovat', 'oder':'nebo', 'r Abend':'večer', 'als':'jako', 'bitten':'prosit',
        'buchstabieren':'hláskovat','wessen':'čí','r Koffer':'kufr','wer':'kdo','wem':'komu',
        'wen':'koho','sehen':'vidět','e Tochter':'dcera','gruSen':'zdravit','warten':'čekat','sagen':'říkat',
        'e Lehrerin':'učitelka','r Lehrer':'učitel','schreiben':'napsat','zeigen':'ukázat','stehen':'stát','suchen':'hledat',
        'fahren':'jet','abfahren':'odjet','kommen':'přijít','hier'and'da':'tady','s Buch':'kniha',
        'r Zug':'vlak','offnen':'otevřít','schlieSen':'zavřít','ab/biegen':'odbočit','e Ampel':'semafor',
        'denn':'pak'and'potom','dorthin':'tam','až'and'dokud':'bis','zu':'k'and'ke','druben'and'gegenuber':'naproti','fremd':'cizí',
        'r FuSganger':'chodec','gerade':'právě','geradeaus':'rovně','e Halstestelle':'zastávka','r Hauptbahnhof':'hlavní nádraží',
        'ihnen':'vám','e Kreuzung':'křižovatka','links':'vlevo','nach links':'doleva','mit':'se'or's','nach':'do'or'po',
        'rechts':'vpravo','e StraSe':'ulice'and'silnice','uberqueren':'přejít','ungefahr':'přibližně'or'asi',
        'von hier':'odsud','weiter':'dál','zu FuS':'pěšky','aber':'ale','alles':'všechno','e Blume':'květina',
        'brav':'hodný','ein bisschen':'trochu','faul':'líný','fleiSig':'pilný','e Freizeit':'volný čas','r FuSball':'fotbal',
        'gern(e)':'rád','groS':'velký','haben':'mít','horen':'poslouchat','hubsch'and'schon':'hezký'or'pěkný','jetzt':'teď'or'nyní',
        'e Journalistin':'novinářka','s Kaninchen':'králík','lernen':'učit se','lieb':'milý','lustig':'veselý',
        'manchmal':'někdy'or'občas','nett':'milý'or'vlídný'or'hezký','noch':'ještě','nur':'jen','oft':'často',
        'recht':'skutečně'or'opravdu'or'velmi','sauber':'čistý','sauber machen':'uklízet','schauen':'dívat se'or'podívat se',
        'schlank':'štíhlý','sehr':'velmi','zehn':'deset','r Spaziergang':'procházka','einen Spaziergang machen':'jít na procházku',
        'spielen':'hrát','studieren':'studovat','s Tier':'zvíře','treiben':'zabývat se'or'provozovat','e Zeit':'čas',
        'Sport treiben':'sportovat','verheiratet':'ženatý'or'vdaná','r Unternhehmer':'podnikatel','zu Hause':'doma',
        'ziemlich':'pořádně'or'značně','zwanzig':'dvacet','aus':'z','dann':'potom','dich':'tebe'or'tě',
        'dir':'ti'or'tobě','e Entschuldigung':'omluva'or'prominutí','finden':'nacházet'or'shledávat','gehen':'jít',
        'geil':'báječný'or'skvělý'or'super','heiSen':'jmenovat se','r Herr':'pán','e Frau':'paní','r Nachname':'příjmení',
        'leider':'bohužel','r Tag':'den','viel':'hodně'and'hodně','was':'co','wie':'jak','woher':'odkud','wohnen':'bydlet',
        'Tschechien':'Česko'
        }
    keyword_list=list(vocabDictionary.keys())
    random.shuffle(keyword_list)
    score=0

    for keyword in keyword_list:
        display='{}'
        print(display.format(keyword))
        userInputAnswer=input(': ')
        print('')
M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
  • whats your output – Seaver Olson Nov 06 '20 at 19:43
  • The words before `:` are called "keys", the words after `:` are called "values". Your `keyword_list` is being made of just the keys. Perhaps you need to do: `keyword_list = list(vocabDictionary.keys()) + list(vocabDictionary.values())`. – Czaporka Nov 06 '20 at 19:46
  • 1
    You can't use `and`/ `or` like that in dictionaries. Considering using a list for the answers if there is more than one, i.e., `or` and having two separate entries for each key if you have `and` on the left hand side of `:` in a entry. I don't understand the logic why you have `and` on the right hand side for some entries, if they are the same word. – Sash Sinha Nov 06 '20 at 19:47

2 Answers2

1

vocabDictionary.keys() This code only returns the keys of a dictionary, which are the words before the :

To create a list containing both the keys and the values, you can use .values() to create another list, and add the two lists

keyword_list1=list(vocabDictionary.keys())
keyword_list2= list(vocabDictionary.values())
keyword_list = keyword_list1 + keyword_list2

Full codes below:

print('Welcome to german vocabulary quiz!')


import random

answer = input('Ready?  ')

print('')

while answer=='y' or 'yes':
    vocabDictionary={
        'e Arbeit':'pracovat', 'oder':'nebo', 'r Abend':'večer', 'als':'jako', 'bitten':'prosit',
        'buchstabieren':'hláskovat','wessen':'čí','r Koffer':'kufr','wer':'kdo','wem':'komu',
        'wen':'koho','sehen':'vidět','e Tochter':'dcera','gruSen':'zdravit','warten':'čekat','sagen':'říkat',
        'e Lehrerin':'učitelka','r Lehrer':'učitel','schreiben':'napsat','zeigen':'ukázat','stehen':'stát','suchen':'hledat',
        'fahren':'jet','abfahren':'odjet','kommen':'přijít','hier'and'da':'tady','s Buch':'kniha',
        'r Zug':'vlak','offnen':'otevřít','schlieSen':'zavřít','ab/biegen':'odbočit','e Ampel':'semafor',
        'denn':'pak'and'potom','dorthin':'tam','až'and'dokud':'bis','zu':'k'and'ke','druben'and'gegenuber':'naproti','fremd':'cizí',
        'r FuSganger':'chodec','gerade':'právě','geradeaus':'rovně','e Halstestelle':'zastávka','r Hauptbahnhof':'hlavní nádraží',
        'ihnen':'vám','e Kreuzung':'křižovatka','links':'vlevo','nach links':'doleva','mit':'se'or's','nach':'do'or'po',
        'rechts':'vpravo','e StraSe':'ulice'and'silnice','uberqueren':'přejít','ungefahr':'přibližně'or'asi',
        'von hier':'odsud','weiter':'dál','zu FuS':'pěšky','aber':'ale','alles':'všechno','e Blume':'květina',
        'brav':'hodný','ein bisschen':'trochu','faul':'líný','fleiSig':'pilný','e Freizeit':'volný čas','r FuSball':'fotbal',
        'gern(e)':'rád','groS':'velký','haben':'mít','horen':'poslouchat','hubsch'and'schon':'hezký'or'pěkný','jetzt':'teď'or'nyní',
        'e Journalistin':'novinářka','s Kaninchen':'králík','lernen':'učit se','lieb':'milý','lustig':'veselý',
        'manchmal':'někdy'or'občas','nett':'milý'or'vlídný'or'hezký','noch':'ještě','nur':'jen','oft':'často',
        'recht':'skutečně'or'opravdu'or'velmi','sauber':'čistý','sauber machen':'uklízet','schauen':'dívat se'or'podívat se',
        'schlank':'štíhlý','sehr':'velmi','zehn':'deset','r Spaziergang':'procházka','einen Spaziergang machen':'jít na procházku',
        'spielen':'hrát','studieren':'studovat','s Tier':'zvíře','treiben':'zabývat se'or'provozovat','e Zeit':'čas',
        'Sport treiben':'sportovat','verheiratet':'ženatý'or'vdaná','r Unternhehmer':'podnikatel','zu Hause':'doma',
        'ziemlich':'pořádně'or'značně','zwanzig':'dvacet','aus':'z','dann':'potom','dich':'tebe'or'tě',
        'dir':'ti'or'tobě','e Entschuldigung':'omluva'or'prominutí','finden':'nacházet'or'shledávat','gehen':'jít',
        'geil':'báječný'or'skvělý'or'super','heiSen':'jmenovat se','r Herr':'pán','e Frau':'paní','r Nachname':'příjmení',
        'leider':'bohužel','r Tag':'den','viel':'hodně'and'hodně','was':'co','wie':'jak','woher':'odkud','wohnen':'bydlet',
        'Tschechien':'Česko'
        }
    keyword_list1=list(vocabDictionary.keys())
    keyword_list2= list(vocabDictionary.values())
    keyword_list = keyword_list1 + keyword_list2
    random.shuffle(keyword_list)
    score=0

    for keyword in keyword_list:
        display='{}'
        print(display.format(keyword))
        userInputAnswer=input(': ')
        print('')
        try:
            if userInputAnswer==(vocabDictionary[keyword]):    
                score += 1
        except KeyError:
            try:
                if keyword == vocabDictionary[userInputAnswer]:
                    score +=1
            except KeyError:
                pass
        
        print(score)
VivianW
  • 41
  • 5
  • Yes! That works, but everytime it switches the word (word behind :) it shows error. Should I change it somehow? `if userInputAnswer==(vocabDictionary[keyword]): score += 1 print(score)` – Nessie CzLP Nov 06 '20 at 20:18
  • When it shows the `values` and the user inputs the `keys`, it will show `KeyError` because `dict[keys]` the word inside the bracket has to be the keys. You need to find the keys in a dictionary using its values. I have attached my full codes above. – VivianW Nov 06 '20 at 20:37
  • hey again, so another error appeared when I get the reversed word wrong (if right it works just fine). During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\natyv\Documents\python prgrm\gvt.py", line 89, in if keyword==vocabDictionary[userInputAnswer]: KeyError: '' – Nessie CzLP Nov 07 '20 at 16:36
0

Currently, you are only picking words from keys (so before the semicolumn).

You could try this:

keyword_list_keys=list(vocabDictionary.keys())
keyword_list_values=list(vocabDictionary.values())
random.shuffle(keyword_list_keys + keyword_list_values)

Then you would have to differentiate depending on the two cases, to find the matching key/value.

Blue Owl
  • 119
  • 12