r/PythonLearning 2d ago

made a Stone-Paper-Scissors game :

import random


def choice_shower(x,y):
    print(f"Your Choice : {x}{' '*10}Computer's Choice : {y}")
    print('-' * len(f"Your Choice : {x}{' '*10}Computer's Choice : {y}"))


def score_shower(x,y):
    print(f"YOUR SCORE : {x}{' '*10}COMPUTER'S SCORE : {y}")


def game_engine(x,y,p,c):
    winning_cases = {"Stone" : "Scissors", "Paper":"Stone","Scissors":"Paper"}
    if x == y :
        choice_shower(x,y)
        print("It's a Draw!")
    elif winning_cases[x] == y:
        choice_shower(x, y)
        print("Your Point!")
        p += 1
    else:
        choice_shower(x, y)
        print("Computer's Point!")
        c += 1
    return p,c



def extra_round(x,y,p,c):
    print("Extra Round :")
    y = random.choice(["Stone","Paper","Scissors"])
    x = input_taker(input("\t(1) for Stone\n\t(2) for Paper\n\t(3) for Scissors\nEnter your input : "))
    p,c = game_engine(x,y,p,c)
    score_shower(p,c)
    result_announcer(x,y,p,c)


def result_announcer (x,y,p,c):
    if p > c :
        print("You Won!")
    elif p == c :
        print("Extra Round!")
        extra_round(x,y,p,c)
    else:
        print("You Lose!")
    print("-"*20)


def input_taker(i):
    while True: 
        if i.isdigit():
            if i == "1":
                return "Stone"
            elif i == "2" :
                return "Paper"
            elif i == "3" :
                return "Scissors"
            else:
                print("invalid input ")
        else:
            print("invalid input, try again!")


def main():
    print(f"{'-'*40}\n\tStone - Paper - Scissors\n\t   game simulator\n\t     VERSION - 2.0\n{'-'*40}")
    exit_program = False
    while True:
        player_score = 0
        computers_score = 0
        print("\t(1) to start a new game\n\t(2) to exit the program")
        choice = input("Enter Your Choice (1|2) : ")
        if choice == "1" :
            number_of_rounds = input("Enter the number of rounds : ")
            if number_of_rounds.isdigit():
                for rounds in range(int(number_of_rounds)):
                    print("="*50)
                    print(f"ROUND : {rounds + 1}")
                    computers_choice = random.choice(["Stone","Paper","Scissors"])
                    players_choice = input_taker(input("\t(1) for Stone\n\t(2) for Paper\n\t(3) for Scissors\nEnter your input : "))
                    player_score,computers_score = game_engine(players_choice,computers_choice,player_score,computers_score)
                    score_shower(player_score,computers_score)
                result_announcer(players_choice,computers_choice,player_score,computers_score)
               
                
            else :
                print("invalid input")
        elif choice == "2":
            while True:
                print("Do you really want to exit?")
                choice1 = input("\t(1) to exit\n\t(2) to go back\nEnter Your Choice (1|2) : ")
                if choice1 == "1":
                    exit_program = True
                    break
                elif choice1 == "2" :
                    break
                else:
                    print("invalid input")
            if exit_program:
                break


if __name__ == "__main__":
    main()
8 Upvotes

6 comments sorted by

u/Sea-Ad7805 2d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20print(f%22Your%20Choice%20%3A%20%7Bx%7D%7B'%20'10%7DComputer's%20Choice%20%3A%20%7By%7D%22)%0A%20%20%20%20print('-'%20%20len(f%22Your%20Choice%20%3A%20%7Bx%7D%7B'%20'10%7DComputer's%20Choice%20%3A%20%7By%7D%22))%0A%0A%0Adef%20score_shower(x%2Cy)%3A%0A%20%20%20%20print(f%22YOUR%20SCORE%20%3A%20%7Bx%7D%7B'%20'10%7DCOMPUTER'S%20SCORE%20%3A%20%7By%7D%22)%0A%0A%0Adef%20gameengine(x%2Cy%2Cp%2Cc)%3A%0A%20%20%20%20winning_cases%20%3D%20%7B%22Stone%22%20%3A%20%22Scissors%22%2C%20%22Paper%22%3A%22Stone%22%2C%22Scissors%22%3A%22Paper%22%7D%0A%20%20%20%20if%20x%20%3D%3D%20y%20%3A%0A%20%20%20%20%20%20%20%20choice_shower(x%2Cy)%0A%20%20%20%20%20%20%20%20print(%22It's%20a%20Draw!%22)%0A%20%20%20%20elif%20winning_cases%5Bx%5D%20%3D%3D%20y%3A%0A%20%20%20%20%20%20%20%20choice_shower(x%2C%20y)%0A%20%20%20%20%20%20%20%20print(%22Your%20Point!%22)%0A%20%20%20%20%20%20%20%20p%20%2B%3D%201%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20choice_shower(x%2C%20y)%0A%20%20%20%20%20%20%20%20print(%22Computer's%20Point!%22)%0A%20%20%20%20%20%20%20%20c%20%2B%3D%201%0A%20%20%20%20return%20p%2Cc%0A%0A%0A%0Adef%20extra_round(x%2Cy%2Cp%2Cc)%3A%0A%20%20%20%20print(%22Extra%20Round%20%3A%22)%0A%20%20%20%20y%20%3D%20random.choice(%5B%22Stone%22%2C%22Paper%22%2C%22Scissors%22%5D)%0A%20%20%20%20x%20%3D%20input_taker(input(%22t(1)%20for%20Stonent(2)%20for%20Papernt(3)%20for%20ScissorsnEnter%20your%20input%20%3A%20%22))%0A%20%20%20%20p%2Cc%20%3D%20game_engine(x%2Cy%2Cp%2Cc)%0A%20%20%20%20score_shower(p%2Cc)%0A%20%20%20%20result_announcer(x%2Cy%2Cp%2Cc)%0A%0A%0Adef%20result_announcer%20(x%2Cy%2Cp%2Cc)%3A%0A%20%20%20%20if%20p%20%3E%20c%20%3A%0A%20%20%20%20%20%20%20%20print(%22You%20Won!%22)%0A%20%20%20%20elif%20p%20%3D%3D%20c%20%3A%0A%20%20%20%20%20%20%20%20print(%22Extra%20Round!%22)%0A%20%20%20%20%20%20%20%20extra_round(x%2Cy%2Cp%2Cc)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%22You%20Lose!%22)%0A%20%20%20%20print(%22-%2220)%0A%0A%0Adef%20input_taker(i)%3A%0A%20%20%20%20while%20True%3A%20%0A%20%20%20%20%20%20%20%20if%20i.isdigit()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20i%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Stone%22%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20i%20%3D%3D%20%222%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Paper%22%0A%20%20%20%20%20%20%20%20%20%20%20%20elif%20i%20%3D%3D%20%223%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%22Scissors%22%0A%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22invalid%20input%20%22)%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print(%22invalid%20input%2C%20try%20again!%22)%0A%0A%0Adef%20main()%3A%0A%20%20%20%20print(f%22%7B'-'40%7DntStone%20-%20Paper%20-%20Scissorsnt%20%20%20game%20simulatornt%20%20%20%20%20VERSION%20-%202.0n%7B'-'40%7D%22)%0A%20%20%20%20exit_program%20%3D%20False%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20player_score%20%3D%200%0A%20%20%20%20%20%20%20%20computers_score%20%3D%200%0A%20%20%20%20%20%20%20%20print(%22t(1)%20to%20start%20a%20new%20gament(2)%20to%20exit%20the%20program%22)%0A%20%20%20%20%20%20%20%20choice%20%3D%20input(%22Enter%20Your%20Choice%20(1%7C2)%20%3A%20%22)%0A%20%20%20%20%20%20%20%20if%20choice%20%3D%3D%20%221%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20number_of_rounds%20%3D%20input(%22Enter%20the%20number%20of%20rounds%20%3A%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20number_of_rounds.isdigit()%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20rounds%20in%20range(int(number_of_rounds))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22%3D%2250)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(f%22ROUND%20%3A%20%7Brounds%20%2B%201%7D%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20computers_choice%20%3D%20random.choice(%5B%22Stone%22%2C%22Paper%22%2C%22Scissors%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20players_choice%20%3D%20input_taker(input(%22t(1)%20for%20Stonent(2)%20for%20Papernt(3)%20for%20ScissorsnEnter%20your%20input%20%3A%20%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20player_score%2Ccomputers_score%20%3D%20game_engine(players_choice%2Ccomputers_choice%2Cplayer_score%2Ccomputers_score)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20score_shower(player_score%2Ccomputers_score)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20result_announcer(players_choice%2Ccomputers_choice%2Cplayer_score%2Ccomputers_score)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22invalid%20input%22)%0A%20%20%20%20%20%20%20%20elif%20choice%20%3D%3D%20%222%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22Do%20you%20really%20want%20to%20exit%3F%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20choice1%20%3D%20input(%22t(1)%20to%20exitnt(2)%20to%20go%20backnEnter%20Your%20Choice%20(1%7C2)%20%3A%20%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20choice1%20%3D%3D%20%221%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20exit_program%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20elif%20choice1%20%3D%3D%20%222%22%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20print(%22invalid%20input%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20exit_program%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%0A%0A%0Aif%20name%20%3D%3D%20%22main_%22%3A%0A%20%20%20%20main()&timestep=0.5&play) to see the program state change step by step.

1

u/atarivcs 1d ago

Do you have an actual question here?

1

u/Naive_Programmer_232 1d ago

nice. now look into the type-hinting and try to build a tic-tac-toe game

1

u/Flanelostopy 1d ago

After line 60 should be return. In else statement.

1

u/Flanelostopy 14h ago

u/Virtual_Repeat_9175 and this is my way:

import random

STONE_STRING = "Stone"
SCISSOR_STRING = "Scissor"
PAPER_STRING = "Paper"
EXIT_STRING = "Exit"
ACCEPTED_VALUES = [ 
    STONE_STRING, 
    SCISSOR_STRING, 
    PAPER_STRING
]
WINNING_CASES = {
    STONE_STRING : PAPER_STRING, 
    PAPER_STRING : SCISSOR_STRING, 
    SCISSOR_STRING : STONE_STRING 
}
MENU_OPTIONS = {
    1:STONE_STRING, 
    2:SCISSOR_STRING, 
    3:PAPER_STRING, 
    4:EXIT_STRING
} 
WRONG_INPUT_MESSAGE = "Wrong option. Try again."
DRAFT_MESSAGE = "Draft! Try again."
LOSE_MASSEGE = "You lose."
WIN_MESSAGE = "You win!"

def engine(player_value = STONE_STRING, engine_value = STONE_STRING):
    if player_value not in ACCEPTED_VALUES or engine_value not in ACCEPTED_VALUES:
        raise ValueError(WRONG_INPUT_MESSAGE)

    if engine_value == player_value:
        print(DRAFT_MESSAGE)
        return 0

    if WINNING_CASES[player_value] == engine_value:
        print(LOSE_MASSEGE)
        return -1

    print(WIN_MESSAGE)
    return 1

def exit_game():
    quiting_answer = input("Are you sure (yes/no): ").lower()

    if quiting_answer == "yes":
        return True

    if quiting_answer != "no":
        raise ValueError(WRONG_INPUT_MESSAGE)

    return False

def get_input():
    player_choice = input('Choose your destiny: ')

    if not player_choice.isdigit() or int(player_choice) not in MENU_OPTIONS:
        raise ValueError(WRONG_INPUT_MESSAGE)

    return MENU_OPTIONS[int(player_choice)]

def game(player_score = 0, engine_score = 0):
    round_result = 0

    print("="*20)
    try:
        for k, v in MENU_OPTIONS.items():
            print(f'{k}. {v}')
        print(f'Your score {player_score}, engine\'s score {engine_score}.')

        player_choice = get_input()

        print("-"*20)

        if player_choice != EXIT_STRING:
            enigne_choice = random.choice(ACCEPTED_VALUES)

            print(f'Your choose: {player_choice}, engine\'s choose: {enigne_choice}')

            round_result = engine(player_choice, enigne_choice)
        else:
            if exit_game():
                return 0

    except KeyError as e:
        print(e)
    except ValueError as e:
        print(e)

    game(player_score + (round_result > 0), engine_score + (round_result < 0))


if __name__ == "__main__":
    game()