16-is-dead/clean_input.py

60 lines
1.8 KiB
Python
Raw Normal View History

2020-12-09 11:24:01 +01:00
"""clean_input"""
2020-12-10 10:13:16 +01:00
import sys
import game
2020-12-09 11:24:01 +01:00
# message needs to be a string that is printed for user instruction. options is a list of viable inputs
def io_str(message: str, options: list) -> str:
while True:
2020-12-09 11:24:01 +01:00
try:
i = str(input(message))
except (ValueError, NameError):
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
if i in options:
return i
2020-12-09 11:24:01 +01:00
break
2020-12-10 10:13:16 +01:00
2020-12-09 11:24:01 +01:00
else:
if i == "q":
print("--------------------------\nSpiel wird beendet...\n--------------------------")
sys.exit()
elif i == "r":
print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
game.main()
else:
print('Noch nicht ganz richtig. Hast du deine Auswahl auch richtig geschrieben?')
def io_int(message: str) -> int:
while True:
unfiltered_input = input(message)
if unfiltered_input == "q":
print("--------------------------\nSpiel wird beendet...\n--------------------------")
sys.exit()
#elif unfiltered_input == "r":
#print("--------------------------\nSpiel wird neu gestartet...\n--------------------------")
#game.main() #DAS FUNKTIONIERT NOCH NICHT
try:
i = int(unfiltered_input)
if i >= 1:
return i
print("deine Zahl ist", i)
break
else:
print('Bitte gib eine ganze Zahl größer als 0 ein. ')
except (ValueError, NameError):
print("Eingabe nicht gültig, bitte wiederhole deine Eingabe.")
#testing
io_int("Bitte gib eine Zahl ein.")