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.
Quartett/clean_input.py

143 lines
3.5 KiB

"""This module handles most user inputs."""
__author__ = ""
__credits__ = ""
__email__ = ""
import sys
import quartett
def io_str(message: str, options: list) -> str:
"""
This method checks if a user input fits the options.
It also handles 'r' for restarting and 'q' for quitting.
:param message: message needs to be a string that is printed for user instruction.
:param options: options is a list of viable inputs.
:return: Cleaned user input
"""
while True:
try:
i = str(input(message))
except (ValueError, NameError):
i = 0
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
if i in options:
return i
else:
if i == "q":
end()
elif i == "r":
restart()
else:
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
def io_int(message: str) -> int:
"""
This method checks if a user input is an integer >1.
It also handles 'r' for restarting and 'q' for quitting.
:param message: message needs to be a string that is printed for user instruction
:return: Integer >1
"""
while True:
unfiltered_input = input(message)
if unfiltered_input == "q":
end()
elif unfiltered_input == "r":
restart()
try:
i = int(unfiltered_input)
if i >= 1:
return i
else:
print('Bitte gib eine ganze Zahl größer als 0 ein. ')
except (ValueError, NameError):
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
def io_card_selection(message: str):
"""
Takes an input message to be printed.
Checks if user input fits the form 0a - 3h.
Construct a card dict to be returned.
:param message: message needs to be a string that is printed for user instruction
:return: card dict
"""
card_stack = define_card_stack()
while True:
user_input = input(message)
if user_input == "":
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
elif len(user_input) > 2:
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
elif user_input[0] in "0123" and user_input[1] in "abcdefgh":
for c in card_stack:
if c["number"] == user_input[0] and c["letter"] == user_input[1]:
return c
elif user_input == "q":
end()
elif user_input == "r":
restart()
else:
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
def define_card_stack():
"""
Defines a card stack for comparing values.
The card stack has the same structure as in
quartett.initialize()
:return: list of dicts
"""
card_stack = []
x = 0
for i in range(4):
for l in "abcdefgh":
d = {"id": str(x), "number": str(i), "letter": l}
card_stack.append(d)
x += 1
return card_stack
def end():
"""
Prints exit screen and quits.
:return: None
"""
print(
"---------------------\nSpiel wird beendet...\n---------------------")
sys.exit()
def restart():
"""
Prints restart screen and restarts the game.
:return: None
"""
print(
"---------------------\nSpiel wird neu gestartet...\n---------------------")
quartett.central_function()