Aug-20-2024, 07:11 PM
input("do you speek german? (ja/no):")
i want to shorten a working section
|
Aug-20-2024, 07:11 PM
input("do you speek german? (ja/no):")
Aug-21-2024, 05:04 AM
(This post was last modified: Aug-21-2024, 11:03 AM by deanhystad.)
Instead of rewriting the game code for each language, write the game code once and translate the displayed text to the desired language while the game is running.
The code below is an example of how this can be achieved using a translation dictionary: import random translations = { "deutch": { "maximum": "maximal", "minimum": "minimal", "invalid value. try again": "ungültiger wert. versuche es noch einmal", "round": "rundden", "how many rounds do you want to play? ": "wie viele runden wilst du speilen? ", "what should the minimum be? ": "was soll das minimum sein? ", "what should the maximum be? ": "was soll das maximum sein? ", "the minimum cannot be more than the maximum": "das minimum kann nicht mehr als das maximum sein", "how many attempts do you want? ": "wie viele versuch wilst du haben? ", "number between": "nummer zwischen", "is correct!": "ist richtig!", "is too much": "ist zu viel", "is too little": "ist zu wenig", "you took too many tries": "du hast zu viele versuche gebraucht", "the number was": "die zahl war", "rounds successfully completed": "unden erfolgreich absolviert", "guesses used: ": "vermutungen verwendet: ", "guesses remaining: ": "verbleibende vermutungen: ", "play again? (yes/no): ": "nochmal spielen? (ja/nein): ", "yes": "ja", "no": "nein", "of": "von", "your average is": "dein durchschnitt ist" } } def _(string): """Translate string from english to selected language.""" return language.get(string, string) def int_input(prompt, value_type="", max_value=None, min_value=None): """Get integer input. use max_value and min_value to limit range of input. """ while True: try: value = int(input(_(prompt))) if max_value is not None and value > max_value: print(_("maximum"), max_value, _(value_type)) elif min_value is not None and value < min_value: print(_("minimum"), min_value, _(value_type)) else: return value except ValueError: print(_("invalid value. try again")) print("----------------------------------") def play_game(average=False): count = 0 rounds = int_input("how many rounds do you want to play? ", "round", max_value=15) for round in range(1, rounds + 1): print("----------------------------------") print(_("round"), round) while True: low = int_input("what should the minimum be? ", min_value=-500000) high = int_input("what should the maximum be? ", max_value=500000) if low > high: print("----------------------------------") print(_("the minimum cannot be more than the maximum")) print("----------------------------------") else: break print("----------------------------------") number = random.randint(low, high) if average: max_guesses = None # Unlimited guesses else: max_guesses = int_input("how many attempts do you want? ", max_value=min(100, high-low)) guesses = 0 while True: guesses += 1 guess = int_input(f"{_('number between')} ({low} - {high}): ", min_value=low, max_value=high) if guess == number: print(guess, _("is correct!")) print(_("guesses used: "), guesses) if average: count += guesses else: count += 1 break if guess > number: print(guess, _("is too much")) high = guess else: print(guess, _("is too little")) low = guess if max_guesses is not None: if guesses >= max_guesses: print(_("you took too many tries")) print(_("the number was"), number) break print(_("guesses remaining: "), max_guesses-guesses) print("----------------------------------") if average: print(_("your average is"), count / rounds) else: print(count, _("of"), rounds, _("rounds successfully completed")) while True: # Select language for instructions. language = translations.get(input("language (english, deutch): "), {}) play_game() print("----------------------------------") if input(_("play again? (yes/no): ")).lower() != _("yes"): breakThe code is awkward looking, and the translation dictionary difficult to write. That is why there are tools to make this easier to do. One tool is gettext. manual page: https://docs.python.org/3/library/gettext.html tutorial: https://phrase.com/blog/posts/translate-...u-gettext/
Aug-21-2024, 11:02 AM
a bit proly translated but ok thx
Aug-21-2024, 06:11 PM
Kann nicht aus dem Wirrwarr schlau werden, kann aber mit dem Deutsch helfen.
Can't help with the chaotic code, can help with the German. (It's too painful!) Quote:translations = {
Aug-23-2024, 11:43 AM
hätte ich selber hingegrigt
Aug-23-2024, 11:56 AM
For pizzakafz the dictionaries should translate from Deutsch to the display language so all the translation keys are in Deutsch.
|
|