You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

clean_input.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """This module handles most user inputs."""
  2. __author__ = ""
  3. __credits__ = ""
  4. __email__ = ""
  5. import sys
  6. import quartett
  7. def io_str(message: str, options: list) -> str:
  8. """
  9. This method checks if a user input fits the options.
  10. It also handles 'r' for restarting and 'q' for quitting.
  11. :param message: message needs to be a string that is printed for user instruction.
  12. :param options: options is a list of viable inputs.
  13. :return: Cleaned user input
  14. """
  15. while True:
  16. try:
  17. i = str(input(message))
  18. except (ValueError, NameError):
  19. i = 0
  20. print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
  21. if i in options:
  22. return i
  23. else:
  24. if i == "q":
  25. end()
  26. elif i == "r":
  27. restart()
  28. else:
  29. print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
  30. def io_int(message: str) -> int:
  31. """
  32. This method checks if a user input is an integer >1.
  33. It also handles 'r' for restarting and 'q' for quitting.
  34. :param message: message needs to be a string that is printed for user instruction
  35. :return: Integer >1
  36. """
  37. while True:
  38. unfiltered_input = input(message)
  39. if unfiltered_input == "q":
  40. end()
  41. elif unfiltered_input == "r":
  42. restart()
  43. try:
  44. i = int(unfiltered_input)
  45. if i >= 1:
  46. return i
  47. else:
  48. print('Bitte gib eine ganze Zahl größer als 0 ein. ')
  49. except (ValueError, NameError):
  50. print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
  51. def io_card_selection(message: str):
  52. """
  53. Takes an input message to be printed.
  54. Checks if user input fits the form 0a - 3h.
  55. Construct a card dict to be returned.
  56. :param message: message needs to be a string that is printed for user instruction
  57. :return: card dict
  58. """
  59. card_stack = define_card_stack()
  60. while True:
  61. user_input = input(message)
  62. if user_input == "":
  63. print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
  64. elif len(user_input) > 2:
  65. print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
  66. elif user_input[0] in "0123" and user_input[1] in "abcdefgh":
  67. for c in card_stack:
  68. if c["number"] == user_input[0] and c["letter"] == user_input[1]:
  69. return c
  70. elif user_input == "q":
  71. end()
  72. elif user_input == "r":
  73. restart()
  74. else:
  75. print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
  76. def define_card_stack():
  77. """
  78. Defines a card stack for comparing values.
  79. The card stack has the same structure as in
  80. quartett.initialize()
  81. :return: list of dicts
  82. """
  83. card_stack = []
  84. x = 0
  85. for i in range(4):
  86. for l in "abcdefgh":
  87. d = {"id": str(x), "number": str(i), "letter": l}
  88. card_stack.append(d)
  89. x += 1
  90. return card_stack
  91. def end():
  92. """
  93. Prints exit screen and quits.
  94. :return: None
  95. """
  96. print(
  97. "---------------------\nSpiel wird beendet...\n---------------------")
  98. sys.exit()
  99. def restart():
  100. """
  101. Prints restart screen and restarts the game.
  102. :return: None
  103. """
  104. print(
  105. "---------------------\nSpiel wird neu gestartet...\n---------------------")
  106. quartett.central_function()