Python: Add types / fix type issues

This commit is contained in:
Martin Thoma
2022-04-02 09:12:13 +02:00
parent 71bae5ded3
commit 68cfd1c751
4 changed files with 10 additions and 10 deletions

View File

@@ -33,7 +33,7 @@ def calculate_score(rolls: List[int]) -> int:
class Player: class Player:
def __init__(self, name: str): def __init__(self, name: str) -> None:
self.name = name self.name = name
self.rolls: List[int] = [] self.rolls: List[int] = []

View File

@@ -10,7 +10,7 @@ Converted from BASIC to Python by Trevor Hobson
class Canvas: class Canvas:
"""For drawing the cookie""" """For drawing the cookie"""
def __init__(self, width=9, height=9, fill="*"): def __init__(self, width=9, height=9, fill="*") -> None:
self._buffer = [] self._buffer = []
for _ in range(height): for _ in range(height):
line = [] line = []
@@ -19,13 +19,13 @@ class Canvas:
self._buffer.append(line) self._buffer.append(line)
self._buffer[0][0] = "P" self._buffer[0][0] = "P"
def render(self): def render(self) -> str:
lines = [" 1 2 3 4 5 6 7 8 9"] lines = [" 1 2 3 4 5 6 7 8 9"]
for row, line in enumerate(self._buffer, start=1): for row, line in enumerate(self._buffer, start=1):
lines.append(" " + str(row) + " " * 5 + " ".join(line)) lines.append(" " + str(row) + " " * 5 + " ".join(line))
return "\n".join(lines) return "\n".join(lines)
def chomp(self, r, c): def chomp(self, r, c) -> str:
if not 1 <= r <= len(self._buffer) or not 1 <= c <= len(self._buffer[0]): if not 1 <= r <= len(self._buffer) or not 1 <= c <= len(self._buffer[0]):
return "Empty" return "Empty"
elif self._buffer[r - 1][c - 1] == " ": elif self._buffer[r - 1][c - 1] == " ":
@@ -39,7 +39,7 @@ class Canvas:
return "Chomp" return "Chomp"
def play_game(): def play_game() -> None:
"""Play one round of the game""" """Play one round of the game"""
players = 0 players = 0
while players == 0: while players == 0:

View File

@@ -61,7 +61,7 @@ def print_instructions() -> None:
print("11 (ELEVEN).\n") print("11 (ELEVEN).\n")
def main(): def main() -> None:
q = random.random() q = random.random()
print("HERE IS THE STARTING LINE OF X'S.\n") print("HERE IS THE STARTING LINE OF X'S.\n")
@@ -75,9 +75,9 @@ def main():
if legal_move: if legal_move:
print(" ".join([str(i) for i in range(1, 11)])) print(" ".join([str(i) for i in range(1, 11)]))
print(" ".join(row[1:]) + "\n") print(" ".join(row[1:]) + "\n")
m = input("INPUT THE NUMBER\n") m_str = input("INPUT THE NUMBER\n")
try: try:
m = int(m) m = int(m_str)
if m > 11 or m < 0: if m > 11 or m < 0:
raise ValueError() raise ValueError()
except ValueError: except ValueError:

View File

@@ -86,12 +86,12 @@ def show_fort_comment(which_fort):
print() print()
def get_yes_or_no(): def get_yes_or_no() -> str:
"""Prompt the player to enter 'YES' or 'NO'. Keep prompting until """Prompt the player to enter 'YES' or 'NO'. Keep prompting until
valid input is entered. Accept various spellings by only valid input is entered. Accept various spellings by only
checking the first letter of input. checking the first letter of input.
Return a single letter 'Y' or 'N'""" Return a single letter 'Y' or 'N'"""
result = 0 result = ""
while result not in ("Y", "N"): while result not in ("Y", "N"):
print("ANSWER YES OR NO") print("ANSWER YES OR NO")
player_choice = input(">> ") player_choice = input(">> ")