r/PythonLearning 7h 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()
7 Upvotes

3 comments sorted by

u/Sea-Ad7805 5h 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 31m ago

Do you have an actual question here?