Skip to content

Commit

Permalink
added type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mattisafur committed Jun 23, 2024
1 parent d0d9b96 commit 8c9c963
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
18 changes: 9 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def welcome() -> None:
player_name = input('Please enter your name: ')
player_name: str = input('Please enter your name: ')
print(f'Hi {player_name} and welcome to the World of Games: The Epic Journey')
return

Expand All @@ -21,25 +21,25 @@ def start_play() -> None:
print(f'{index + 1}. {game.name} - {game.short_description}')

while True:
game_number = input('> ')
game_number = input_validation.validate_int_in_range(game_number, 1, len(
game_number: str = input('> ')
parsed_game_number: int | None = input_validation.validate_int_in_range(game_number, 1, len(
games), input_not_number_prompt='Please enter a number', input_not_int_prompt='Please enter an integer', input_not_in_range_prompt='Input is not a valid option')
if game_number is not None:
if parsed_game_number is not None:
break
game = games[game_number - 1]
game = games[parsed_game_number - 1]

# difficulty selection
print('Please select a difficulty:')
for index, difficulty in enumerate(game.difficulties):
print(f'{index + 1}. {difficulty}')

while True:
difficulty_level = input('> ')
difficulty_level = input_validation.validate_int_in_range(difficulty_level, 1, len(
difficulty_level: str = input('> ')
parsed_difficulty_level: int | None = input_validation.validate_int_in_range(difficulty_level, 1, len(
game.difficulties), input_not_number_prompt='Please enter a number', input_not_int_prompt='Please enter an integer', input_not_in_range_prompt='Input is not a valid option')
if difficulty_level is not None:
if parsed_difficulty_level is not None:
break
difficulty = game.difficulties[difficulty_level - 1]
difficulty = game.difficulties[parsed_difficulty_level - 1]

# start selected game
game.play(difficulty)
29 changes: 15 additions & 14 deletions games/currency_roulette_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import credentials
import input_validation

name = 'Currency Roulette'
short_description = 'Try and guess the value of a random amount of USD in ILS'
full_description = '''An amount in USD between 1 and 100 will be chosen at random.
name: str = 'Currency Roulette'
short_description: str = 'Try and guess the value of a random amount of USD in ILS'
full_description: str = '''An amount in USD between 1 and 100 will be chosen at random.
You must enter an amount in shekels equal to the given amount.
The error margin allowed in your answer depends on the difficulty level selected.'''
difficulties = [
difficulties: list[str] = [
'easy',
'easy-medium',
'medium',
Expand All @@ -18,10 +18,10 @@


def play(difficulty: str) -> bool:
error_margin = 10 - (difficulties.index(difficulty) + 1)
error_margin: int = 10 - (difficulties.index(difficulty) + 1)
if error_margin < 0:
raise ValueError('Difficulty value too high, negative error margin.')
amount = random.randint(1, 100)
amount: int = random.randint(1, 100)

# print intro
print(name)
Expand All @@ -32,28 +32,29 @@ def play(difficulty: str) -> bool:

print(f'The amount to convert is: {amount} USD', end='\n\n')

answer_range = get_money_interval(amount, error_margin)
guess = get_guess_from_user()
answer_range: tuple[float, float] = get_money_interval(
amount, error_margin)
guess: float = get_guess_from_user()

return compare_results(guess, answer_range)


def get_money_interval(number: int, error_margin: int) -> tuple[float, float]:
exchange_rates = currencyapicom.Client(
credentials.Currency_api_key).latest()
ils_exchange_rate = exchange_rates.get(
ils_exchange_rate: float = exchange_rates.get(
'data', {}).get('ILS', {}).get('value')
converted_number = number * ils_exchange_rate
converted_number: float = number * ils_exchange_rate

return (converted_number - error_margin, converted_number + error_margin)


def get_guess_from_user() -> float:
while True:
guess = input('Enter your guess: ')
guess = input_validation.validate_float(guess)
if guess is not None:
return guess
guess: str = input('Enter your guess: ')
parsed_guess: float | None = input_validation.validate_float(guess)
if parsed_guess is not None:
return parsed_guess


def compare_results(user_input: float, answer_range: tuple[float, float]) -> bool:
Expand Down
25 changes: 13 additions & 12 deletions games/guess_game.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import random
import input_validation

name = 'Guess Game'
short_description = 'Guess a number and see if you chose like the computer.'
full_description = '''A random number, 0 or higher will be selected by the game. you must guess that number.
name: str = 'Guess Game'
short_description: str = 'Guess a number and see if you chose like the computer.'
full_description: str = '''A random number, 0 or higher will be selected by the game. you must guess that number.
The range of number the game will choose from depends on the difficulty level selected.'''
difficulties = [
difficulties: list[str] = [
'easy',
'easy-medium',
'medium',
Expand All @@ -15,7 +15,7 @@


def play(difficulty: str) -> bool:
difficulty_level = difficulties.index(difficulty) + 1
difficulty_level: int = difficulties.index(difficulty) + 1

# print intro
print(name)
Expand All @@ -24,8 +24,8 @@ def play(difficulty: str) -> bool:
print(
f'Difficulty {difficulty} - number chosen between 0 and {difficulty_level}')

number = generate_number(difficulty_level)
guess = get_guess_from_user(difficulty_level)
number: int = generate_number(difficulty_level)
guess: int = get_guess_from_user(difficulty_level)

return compare_results(guess, number)

Expand All @@ -36,12 +36,13 @@ def generate_number(max_number: int) -> int:

def get_guess_from_user(max_number: int) -> int:
while True:
guess = input(f'please enter a number between 0 and {max_number}: ')
guess = input_validation.validate_int_in_range(guess, 0, max_number, input_not_number_prompt='Please enter a number',
input_not_int_prompt='Please enter an integer', input_not_in_range_prompt='Input is not in the specified range')
if guess is not None:
guess: str = input(
f'please enter a number between 0 and {max_number}: ')
parsed_guess: int | None = input_validation.validate_int_in_range(guess, 0, max_number, input_not_number_prompt='Please enter a number',
input_not_int_prompt='Please enter an integer', input_not_in_range_prompt='Input is not in the specified range')
if parsed_guess is not None:
break
return guess
return parsed_guess


def compare_results(user_guess: int, number: int) -> bool:
Expand Down
28 changes: 14 additions & 14 deletions games/memory_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import time
import input_validation

name = 'Memory Game'
short_description = 'A sequence of numbers will appear for 1 second and you have to guess it back.'
full_description = '''A sequence of number will be randomly generated and be shown on the screen for 0.7 seconds.
name: str = 'Memory Game'
short_description: str = 'A sequence of numbers will appear for 1 second and you have to guess it back.'
full_description: str = '''A sequence of number will be randomly generated and be shown on the screen for 0.7 seconds.
You must type back the sequence of numbers, one number at a time.
The length of the sequence will depend of the difficulty chosen.'''
difficulties = [
difficulties: list[str] = [
'easy',
'easy-medium',
'medium',
Expand All @@ -18,8 +18,8 @@


def play(difficulty: str) -> bool:
sequence_length = difficulties.index(difficulty) + 1
sequence = generate_sequence(sequence_length)
sequence_length: int = difficulties.index(difficulty) + 1
sequence: list[int] = generate_sequence(sequence_length)

# Print intro
print(name)
Expand All @@ -42,29 +42,29 @@ def play(difficulty: str) -> bool:
time.sleep(0.7)
print('\x1b[2K')

user_sequence = get_list_from_user(sequence_length)
user_sequence: list[int] = get_list_from_user(sequence_length)

return is_list_equal(sequence, user_sequence)


def generate_sequence(length: int) -> list[int]:
sequence = []
sequence: list[int] = []
for _ in range(length):
sequence.append(random.randint(1, 101))
return sequence


def get_list_from_user(amount_of_numbers: int) -> list[int]:
user_sequence = []
user_sequence: list[int] = []

# get number from user and append it to the sequence
number_id = 1
number_id: int = 1
while number_id <= amount_of_numbers:
number = input(f'number {number_id} > ')
number = input_validation.validate_int(
number: str = input(f'number {number_id} > ')
parsed_number: int | None = input_validation.validate_int(
number, input_not_number_prompt='please enter a number', input_not_int_prompt='please enter positive whole numbers only')
if number is not None:
user_sequence.append(number)
if parsed_number is not None:
user_sequence.append(parsed_number)
number_id += 1
else:
continue
Expand Down
6 changes: 3 additions & 3 deletions input_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def validate_int_in_range(string: str, min: int, max: int, input_empty_prompt: s

# input not an int
try:
result = int(string)
result: int = int(string)
except ValueError:
if input_not_int_prompt is not None:
print(input_not_in_range_prompt)
Expand Down Expand Up @@ -46,7 +46,7 @@ def validate_int(string: str, input_empty_prompt: str | None = None, input_not_n
return None

try:
result = int(string)
result: int = int(string)
except ValueError:
if input_not_int_prompt is not None:
print(input_not_int_prompt)
Expand All @@ -64,7 +64,7 @@ def validate_float(string: str, input_empty_prompt: str | None = None, input_not

# input is not a float (or any number for that matter)
try:
result = float(string)
result: float = float(string)
except ValueError:
if input_not_float_prompt is not None:
print(input_not_float_prompt)
Expand Down

0 comments on commit 8c9c963

Please sign in to comment.