Browse Source

mehr aufgaben, zufall

Thomas Verchow 4 years ago
parent
commit
16f5db863e
7 changed files with 189 additions and 77 deletions
  1. 1 0
      .gitignore
  2. 8 0
      run.sh
  3. 3 3
      setup.cfg
  4. 48 0
      src/hanna_rechnet/aufgaben.py
  5. 0 74
      src/hanna_rechnet/hanna-rechnet.py
  6. 129 0
      src/hanna_rechnet/hanna_rechnet.py
  7. 0 0
      videos.txt

+ 1 - 0
.gitignore

@@ -48,3 +48,4 @@ MANIFEST
 
 # Per-project virtualenvs
 .venv*/
+venv.*/

+ 8 - 0
run.sh

@@ -0,0 +1,8 @@
+#!/bin/sh
+
+VENV=./venv.hanna_rechnet
+
+python3 -m venv $VENV
+
+$VENV/bin/pip install -e .
+

+ 3 - 3
setup.cfg

@@ -30,7 +30,7 @@ package_dir =
 # DON'T CHANGE THE FOLLOWING LINE! IT WILL BE UPDATED BY PYSCAFFOLD!
 setup_requires = pyscaffold>=3.2a0,<3.3a0
 # Add here dependencies of your project (semicolon/line-separated), e.g.
-# install_requires = numpy; scipy
+install_requires = readchar
 # The usage of test_requires is discouraged, see `Dependency Management` docs
 # tests_require = pytest; pytest-cov
 # Require a specific Python version, e.g. Python 2.7 or >= 3.4
@@ -52,8 +52,8 @@ testing =
 
 [options.entry_points]
 # Add here console scripts like:
-# console_scripts =
-#     script_name = hanna_rechnet.module:function
+ console_scripts =
+     hanna_rechnet = hanna_rechnet.hanna_rechnet:main
 # For example:
 # console_scripts =
 #     fibonacci = hanna_rechnet.skeleton:run

+ 48 - 0
src/hanna_rechnet/aufgaben.py

@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+
+from random import randint, choice
+
+
+def bis_zum_naechsten_zehner():
+    x = randint(1, 8)
+    y = randint(1, 9)
+
+    aufgabe = f"Von { 10 * x + y } zum nächsten Zehner?"
+    ergebnis = 10 - y
+
+    return aufgabe, ergebnis
+
+
+def minus_einstellig_ueber_zehnergrenze_hinweg_unter_100():
+    x = randint(1, 9)
+    y = randint(x, 9)
+    z = randint(1, 9)
+
+    aufgabe = f"{ z }{ x } - { y } = ?"
+    ergebnis = (10 * z + x) - y
+
+    return aufgabe, ergebnis
+
+
+def minus_einstellig_unter_100():
+    y = randint(1, 9)
+    x = randint(y, 99)
+
+    aufgabe = f"{ x } - { y } = ?"
+    ergebnis = x - y
+
+    return aufgabe, ergebnis
+
+
+def plus_10_bis_19_unter_100():
+    return _plus(range(1, 80), range(10, 19))
+
+
+def _plus(summand_1: list, summand_2: list):
+    x = choice(summand_1)
+    y = choice(summand_2)
+
+    aufgabe = f"{ x } + { y } = ?"
+    ergebnis = x + y
+
+    return aufgabe, ergebnis

+ 0 - 74
src/hanna_rechnet/hanna-rechnet.py

@@ -1,74 +0,0 @@
-#!/usr/bin/python3
-
-import sys
-import random
-import readchar
-import subprocess
-from datetime import datetime
-import logging
-import logging
-logging.basicConfig(level=logging.DEBUG)
-
-videos_filename = 'videos.txt'
-
-def play_video():
-  global videos
-  if len(videos) < 1:
-    with open(videos_filename, 'r') as fh:
-      videos = fh.readlines()
-    random.shuffle(videos)
-    logging.info(f"{len(videos)} videos geladen")
-
-  logging.debug(f"noch { len(videos) } videos vorhanden") 
-  subprocess.run(["mpv", "--fs", videos.pop()])
-
-
-def bis_zum_naechsten_zehner():
-  x = random.randint(1,8)
-  y = random.randint(1,9)
-  print("von {} zum n Zehner?".format(10 * x + y))
-  c = readchar.readkey()
-
-  if c == "x":
-    return "x"
-
-  try:
-    e = (y + int(c)) % 10
-  except:
-    e = 1
-
-  if e == 0:
-    return True
-  else:
-    return False
-
-
-videos = []
-zeiten = []
-
-while True:
-
-  start = datetime.now()
-  e = bis_zum_naechsten_zehner()
-  ende = datetime.now()
-
-  if e == "x":
-    break
-
-  logging.info(zeiten)
-
-  if e:
-    print("RICHTIG")
-    zeiten.insert(0, (ende - start).total_seconds())
-  else:
-    print("FALSCH")
-    zeiten = []
-
-  zeiten = zeiten[0:10]
-
-  logging.info(f"{len(zeiten)} Richtige in {sum(zeiten)} Sekunden")
-
-  if len(zeiten) >= 10 and sum(zeiten) <= 30:
-    print("JUHU!")
-    play_video()
-    zeiten = []

+ 129 - 0
src/hanna_rechnet/hanna_rechnet.py

@@ -0,0 +1,129 @@
+#!/usr/bin/python3
+
+import subprocess
+from os import system
+from sys import exit
+from math import log10
+from time import sleep
+from random import choice, shuffle, seed
+from readchar import readkey
+from datetime import datetime
+from inspect import getmembers, isfunction
+
+import logging
+
+logging.basicConfig(level=logging.WARNING)
+
+from . import aufgaben
+
+seed()
+
+videos_filename = "videos.txt"
+anzahl_aufgaben = 10
+zeit_zur_verfuegung = 60
+
+videos = []
+
+
+def stelle_aufgabe(aufgabe, ergebnis):
+    logging.debug(f"Aufgabe: { aufgabe }")
+    logging.debug(f"Ergebnis: { ergebnis }")
+
+    print(str(aufgabe))
+
+    eingabe = []
+    for i in range(0, 1 + int(log10(ergebnis))):
+        eingabe.append(readkey())
+        if eingabe[-1] == "x" or eingabe[-1] == "q":
+            print("ENDE")
+            exit(0)
+
+    logging.debug(f"Eingabe: { str(eingabe) }")
+
+    wert = 0
+    potenz = 0
+    try:
+        while eingabe:
+            wert += int(eingabe.pop()) * 10 ** potenz
+            potenz += 1
+    except:
+        logging.warning("Kann Eingabe nicht in Wert wandeln.")
+        wert = None
+
+    logging.debug(f"Eingabewert: { wert }")
+
+    return wert == ergebnis
+
+
+def play_video():
+    global videos
+    if len(videos) < 1:
+        with open(videos_filename, "r") as fh:
+            videos = fh.readlines()
+        shuffle(videos)
+        logging.info(f"{len(videos)} videos geladen")
+
+    logging.debug(f"noch { len(videos) } videos vorhanden")
+    subprocess.run(["mpv", "--fs", videos.pop()])
+
+
+def frage_weiter():
+    print("Taste drücken, wenn es weiter gehen soll. Oder 'q' fuer ENDE.")
+    k = readkey()
+    if k == "q":
+        print("ENDE")
+        exit(0)
+
+
+def main():
+    zeiten = []
+
+    aufgaben_vorhanden = []
+    for i in getmembers(aufgaben, isfunction):
+        if i[0][0] != "_":  # remove _internal functions
+            aufgaben_vorhanden.append(i)
+
+    while True:
+        system("clear")
+
+        aufgabe_name, aufgabe_function = choice(aufgaben_vorhanden)
+        aufgabe, ergebnis = aufgabe_function()
+
+        gebraucht = int(sum(zeiten))  # fuer anzahl_aufgaben!
+
+        balken = ["-"] * int(zeit_zur_verfuegung * 1.25)
+        balken[0:gebraucht] = ["+"] * gebraucht
+        balken[zeit_zur_verfuegung] = "|"
+        print("({:2d}) {}".format(len(zeiten), "".join(balken)))
+        print("")
+
+        start = datetime.now()
+        e = stelle_aufgabe(aufgabe, ergebnis)
+        ende = datetime.now()
+
+        if e:
+            print("RICHTIG!")
+            zeiten.insert(0, (ende - start).total_seconds())
+            sleep(1)
+        else:
+            print("FALSCH!\n")
+            print(f"Richtig wäre { ergebnis }.")
+            zeiten = []
+            sleep(2)
+
+        zeiten = zeiten[0:anzahl_aufgaben]
+        logging.debug("Zeiten: { str(zeiten) }")
+
+        logging.info(f"{len(zeiten)} Richtige in { gebraucht } Sekunden.")
+
+        if len(zeiten) >= anzahl_aufgaben and sum(zeiten) <= zeit_zur_verfuegung:
+            print(
+                f"\n\nJUHU! Für die letzten { anzahl_aufgaben } Aufgaben hast du { int(sum(zeiten) + .5) } Sekunden gebraucht!\n"
+            )
+            play_video()
+            zeiten = []
+            frage_weiter()
+
+
+if __name__ == "__main__":
+    main()

+ 0 - 0
videos.txt