Advertisement
DeaD_EyE

Wahlprogramm das man besser nicht verwendet...

May 13th, 2020
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. from pathlib import Path
  2. from random import shuffle
  3. from collections import defaultdict
  4. from operator import itemgetter
  5. from argparse import ArgumentParser
  6.  
  7.  
  8. """
  9. Text der beschreiben soll was das Programm macht.
  10. """
  11.  
  12.  
  13. CLEAR_SCREEN = "\x1b[2J\x1b[H"
  14.  
  15.  
  16. def get_candidates(file):
  17.     with open(file) as fd:
  18.         for line in map(str.strip, fd):
  19.             if line:
  20.                 yield line.strip().title()
  21.  
  22.  
  23. def make_dict(candidates):
  24.     random_candidates = candidates.copy()
  25.     shuffle(random_candidates)
  26.     return {idx: candidate for idx, candidate in enumerate(random_candidates, start=1)}
  27.  
  28.  
  29. def ask(candidates_seq):
  30.     results = defaultdict(int)
  31.     while True:
  32.         candidates = make_dict(candidates_seq)
  33.         print("Es stehen folgende Kandidaten zur Wahl:")
  34.         for idx, candidate in candidates.items():
  35.             print(f"{idx:<3d} -> {candidate}")
  36.         while True:
  37.             user_input = input(
  38.                 "Bitte Kandidaten-Nr. zur Wahl eingeben oder q zum beenden: "
  39.             )
  40.             if user_input.strip().lower() == "q":
  41.                 return results
  42.             try:
  43.                 value = int(user_input)
  44.             except ValueError:
  45.                 print("Bitte eine Ganzzahl eingeben.")
  46.                 continue
  47.             if value not in candidates:
  48.                 print(f"Einen Kandidaten mit der Nummer {value} gibt es nicht.")
  49.             else:
  50.                 results[candidates[value]] += 1
  51.                 print(CLEAR_SCREEN)
  52.                 break
  53.  
  54.  
  55. def output_results(results):
  56.     candidates = [
  57.         candidate
  58.         for candidate, _ in sorted(results.items(), key=itemgetter(1), reverse=True)
  59.     ]
  60.     for candidate in candidates:
  61.         print(f"{candidate} hat {results[candidate]} Stimmen bekommen")
  62.  
  63.  
  64. def parse():
  65.     parser = ArgumentParser(
  66.         description="Ein ganz tolles Wahlprogramm, dass niemand benutzen möchte."
  67.     )
  68.     parser.add_argument(
  69.         "kandidaten", type=Path, help="Datei mit Kandidaten. Ein Kandidat pro Zeile."
  70.     )
  71.     args = parser.parse_args()
  72.     if not args.kandidaten.exists():
  73.         raise RuntimeError("Datei Existiert nicht")
  74.     return args
  75.  
  76.  
  77. if __name__ == "__main__":
  78.     args = parse()
  79.     print(CLEAR_SCREEN)
  80.     try:
  81.         candidates = list(get_candidates(args.kandidaten))
  82.     except RuntimeError as e:
  83.         print(e)
  84.     else:
  85.         results = ask(candidates)
  86.         output_results(results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement