Tutorial OOP Lösungen

Zurück zum Dossier: Tutorial OOP

voci_trainer_v01.py
import random
import os
import sys
import msvcrt # needed for single key input
 
class WordPair:
    def __init__(self, wordL1, wordL2):
        self.wordL1 = wordL1
        self.wordL2 = wordL2
 
 
Voci = [
    WordPair("one", "Eins"),
    WordPair("two", "Zwei"),
    WordPair("three", "Drei"),
    WordPair("four", "Vier"),
    WordPair("fife", "Fuenf")
]
 
run = True
while run:
    # Pick random word
    wp = random.choice(Voci)
 
    # Repeat random word (while wrong)
    repeatWord = True
    while repeatWord:
        os.system('cls||clear') # clear screen
        print(f"English word: {wp.wordL1}") # word to translate
        inp = input(f"German translation: ") # get input
 
        # Check input:
        if inp == wp.wordL2:
            # if correct, break inner while loop and move on to next word
            input(f"Correct! Type Enter for next word.")
            repeatWord = False
        else:
            # if wrong: multiple options
            print(f'Wrong! Type:\n'
                '- (1) to try again\n'
                '- (2) show translation\n'
                '- (3) next word\n'
                '- (4) to quit')
 
            # get a single key input
            key = msvcrt.getch()
            key = key.decode('utf-8').lower() # convert to utf-8 format and to lower case
            if key == '1':
                repeatWord = True
            elif key == '2':
                print(f"Correct word would be: {wp.wordL2}")
                print("press any key to continue")
                msvcrt.getch()
                repeatWord = False
            elif key == '3':
                repeatWord = False
            else:
                repeatWord = False
                run = False
  • talit/tutorial_oop_lsg.1573460661.txt.gz
  • Zuletzt geändert: 2019-11-11 08:24
  • von sca