3.5 מודולים פתרון
מחשבון 4¶
- main.py
- calculator.py
from handlers import handle_calculate, handle_input def welcome() -> None: """ welcoming the user """ welcome_message = "Amit calculator! \n" \ "syntax: > number operation number \n" \ "example: 1 + 3 \n" \ "avilable operations: +,-,*,/ \n" \ "enter /q for exit the program!" print(welcome_message) def start_calculator() -> None: while True: first_number, operation, second_number = handle_input() if operation == "quit": return result = handle_calculate(first_number, operation, second_number) print(f"res: {result}") - handlers.py
from operations import add, sub, mul, div def handle_input() -> (int, str, int): while True: command = input("> ") if command == "/q": return 0, "quit", 0 try: first_number, operation, second_number = command.split(" ") first_number, second_number = int(first_number), int(second_number) return first_number, operation, second_number except Exception: print("Please enter valid expression!") def handle_calculate(first_number: int, operation: str, second_number: int) -> int: try: result = calculate(first_number, operation, second_number) except ZeroDivisionError: print("Can't divide by zero! ") result = 0 except KeyError: print(f"Operation {operation} doesn't exist!") result = 0 finally: return result def calculate(first_number: int, operation: str, second_number: int) -> int: """ calculate result by given 2 integers and operation -> can be only +,-,*,/ """ actions = { "+": add, "-": sub, "*": mul, "/": div } function = actions[operation] result = function(first_number, second_number) return result - operations.py
def add(first_number: int, second_number: int) -> int: """ Adding 2 given parametrs and return """ return first_number + second_number def sub(first_number: int, second_number: int) -> int: """ Subtracting 2 given parameters and return """ return first_number - second_number def mul(first_number: int, second_number: int) -> int: """ Multiplying 2 given parameters and return """ return first_number * second_number def div(first_number: int, second_number: int) -> float: """ Dividing 2 given parameters and return """ return first_number / second_number
אבן נייר ומספריים 2¶
- main.py
""" Rock, Scissor and paper Game """ from game import handle_game def main() -> None: """ Main function :return: None """ print("Welcome to Rock, Paper, Scissors game!\nTo quit the game enter 'quit' or 'q'.") computer_wins = 0 human_wins = 0 final_message = handle_game(computer_wins, human_wins) print(f"\nFinal score: {final_message}") if __name__ == "__main__": main() - game.py
from game_functions import print_scores, determine_winner, handle_user_choice, get_computer_choice def handle_game(human_wins: int, computer_wins: int) -> str: """ handle the game main loop :param human_wins: current human win score :param computer_wins: current computer win score :return: final game message (the winner / draw) """ while True: print_scores(computer_wins, human_wins) computer_choice = get_computer_choice() user_choice = handle_user_choice() if user_choice == "quit" or user_choice == "q": final_message = determine_winner(computer_wins, human_wins) return final_message human_wins, computer_wins = play_round(user_choice, computer_choice, computer_wins, human_wins) def play_round(player1_choice: str, player2_choice: str, player1_score: int, player2_score: int) -> (int, int): """ handle game round :param player1_choice: player 1 choice (rock, paper, scissors) :param player2_choice: player 2 choice (rock, paper, scissors) :param player1_score: player 1 score :param player2_score: player 2 score :return: (player_1_score, player_2_score) """ choices = ["rock", "paper", "scissors"] if player1_choice not in choices: print("Invalid choice. Please enter Rock, Paper, or Scissors.") return print(f"\nHuman: {player1_choice}") print(f"Computer: {player2_choice}") return handle_winner(player1_choice, player2_choice, player1_score, player2_score) def handle_winner(player1_choice: str, player2_choice: str, player1_score: int, player2_score: int) -> (int, int): """ handle game winner :param player1_choice: player 1 choice (rock, paper, scissors) :param player2_choice: player 2 choice (rock, paper, scissors) :param player1_score: player 1 score :param player2_score: player 2 score :return: (player_1_score, player_2_score) """ winner = determine_winner(player2_choice, player1_choice) if winner == "Draw": print("It's a draw!") else: print(f"{winner} won!") if winner == "Computer": player1_score += 1 else: player2_score += 1 return player2_score, player1_score - game_functions.py
import random def print_scores(computer_wins, human_wins) -> None: print(f"\nScores: Computer - {computer_wins} wins, Human - {human_wins} wins.\n") def determine_winner(computer_choice: str, human_choice: str) -> str: """ determine the winner by choices :param computer_choice: computer choice (rock, paper, scissors) :param human_choice: human choice (rock, paper, scissors) :return: winner / draw """ if computer_choice == human_choice: return "Draw" elif ( (computer_choice == "rock" and human_choice == "scissors") or (computer_choice == "scissors" and human_choice == "paper") or (computer_choice == "paper" and human_choice == "rock") ): return "Computer" else: return "Human" def handle_user_choice() -> str: user_choice = get_user_choice() while True: if user_choice not in ["rock", "paper", "scissors", "q", "quit"]: print("Invalid input!") user_choice = get_user_choice() else: return user_choice def get_user_choice() -> str: return input("Enter (Rock, Paper, or Scissors): ").lower() def get_computer_choice() -> str: choices = ["rock", "paper", "scissors"] return random.choice(choices)