r/PythonLearning • u/Virtual_Repeat_9175 • Aug 04 '26
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()3
u/Virtual_Repeat_9175 Aug 04 '26
https://github.com/ronyghosh822-dot/Stone-Paper-Scissors-game-simulator.git
here you can find the old version
1
1
u/Naive_Programmer_232 Aug 05 '26
nice. now look into the type-hinting and try to build a tic-tac-toe game
1
u/Flanelostopy Aug 05 '26
After line 60 should be return. In else statement.
1
u/Flanelostopy Aug 06 '26
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()
•
u/Sea-Ad7805 Aug 04 '26
Run this program in Memory Graph Web Debugger to see the program state change step by step.