Python Forum
2nd Project: Rock, Paper, Scissors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2nd Project: Rock, Paper, Scissors
#1
Hello Smile

My second project is rock, paper, scissors which works with a "best of 3 rounds" mechanic.

Again, if anyone has a little time to spare could you go over my code to see how I've done please? I think my main concern is, have I over-complicated the process? I can't help but think 146 lines is overkill for rock, paper, scissors...?

Any input is very much appreciated, thank you.

import random
import time
import sys

# Options the computer can choose
PCrps = [
    'Rock',
    'Paper',
    'Scissors'
]

# Dictionary to convert player's guess so that it can be compared to computer's guess
guessConvert = {
    'Paper' : 'Rock',
    'Rock' : 'Scissors',
    'Scissors' : 'Paper',
}

# Created to reduce repetition later
def guessRoutine(usr, pc):
    print('You guessed >> ', end='', flush=True)
    time.sleep(1.5)
    print(usr)
    time.sleep(1.5)
    print('Computer guessed >> ', end='', flush=True)
    time.sleep(1.5)
    print(pc)
    time.sleep(.5)

# First fuction to run, the introduction
def intro(q):
    print("Welcome to Rock, Paper, Scissors")
    time.sleep(1)
    print("Best of 3 rounds")
    time.sleep(1)
    print('Round 1')
    score1 = round1(q) # Counts the player's score by returing values from functions.
    score2 = round2(score1) # It works but could get messy if there were numerous iterations.
#    print(score2)      For testing
    if score2 == 0:
        lost()
    elif score2 == 1:
        round3()
    else:
        won()

def round1(q):
    while True:
        randrps = random.randint(0,2)
        pcguess = PCrps[randrps]
#        print(pcguess)     For testing
        usrguess = (input('>>> ')).capitalize()
        if usrguess in PCrps:
            if usrguess == pcguess:
                guessRoutine(usrguess, pcguess)
                print('Draw! Try again\n')
            elif guessConvert[usrguess] == pcguess:
                guessRoutine(usrguess, pcguess)
                print('You won Round 1!\n')
                time.sleep(2)
                return q + 1
            elif guessConvert[usrguess] != pcguess:
                guessRoutine(usrguess, pcguess)
                print('Oh dear! You lost!\n')
                return q
        else:
             print('Please type either "rock", "paper" or "scissors"\n')


def round2(q):
    if q == 1:
        print('Round 2! Beat the computer and win the game! No pressure...')
    else:
        print('Round 2! You must win this round to be in with a chance of winning the game.')
    while True:
        randrps = random.randint(0,2)
        pcguess = PCrps[randrps]
#        print(pcguess)     For testing
        usrguess = (input('>>> ')).capitalize()
        if usrguess in PCrps:
            if usrguess == pcguess:
                guessRoutine(usrguess, pcguess)
                print('Draw! Try again\n')
            elif guessConvert[usrguess] == pcguess:
                guessRoutine(usrguess, pcguess)
                print('You won Round 2!\n')
                time.sleep(2)
                return q + 1
            elif guessConvert[usrguess] != pcguess:
                guessRoutine(usrguess, pcguess)
                print('Oh dear! You lost!\n')
                return q
        else:
             print('Please type either "rock", "paper" or "scissors"\n')

def round3():
    print('Round 3! Winner takes all!')
    while True:
        randrps = random.randint(0,2)
        pcguess = PCrps[randrps]
#        print(pcguess)     For testing
        usrguess = (input('>>> ')).capitalize()
        if usrguess in PCrps:
            if usrguess == pcguess:
                guessRoutine(usrguess, pcguess)
                print('Draw! Try again\n')
            elif guessConvert[usrguess] == pcguess:
                guessRoutine(usrguess, pcguess)
                print('You won Round 3!')
                time.sleep(2)
                won()
            elif guessConvert[usrguess] != pcguess:
                guessRoutine(usrguess, pcguess)
                print('Oh dear! You lost!\n')
                lost()
        else:
             print('Please type either "rock", "paper" or "scissors"\n')

# Funtion runs if player wins
def won():
    print('...and', end='', flush=True)
    time.sleep(1.5)
    print(' you won the game! Congratulations!')
    time.sleep(2)
    while True:
        playagain = input('Play again? y/n\n>>> ')
        if playagain.lower() == 'y':
            intro(0)
        elif playagain.lower() == 'n':
            sys.exit()
        else:
            print('Please type "y" or "n"')

# Function runs if player loses
def lost():
    while True:
        playagain = input('\nPlay again? y/n\n>>> ')
        if playagain.lower() == 'y':
            intro(0)
        elif playagain.lower() == 'n':
            sys.exit()
        else:
            print('Please type "y" or "n"')

# This runs the whole game, and initialises 'q' with 0 to reset the player's score
intro(0)
Thanks for reading. Smile
Reply
#2
I think you did good. It's not that much more lines of code than the one I did.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Well done.
whenever you notice yourself copying and pasting blocks of code ask yourself if you really need to.

functions round1, round2 & round3 are very much the same code,
You could manage with just one function that alters only where needed.

functions won and lost also have a lot of duplicated code.
Reply
#4
(Jun-04-2020, 06:48 PM)Yoriz Wrote: Well done.
whenever you notice yourself copying and pasting blocks of code ask yourself if you really need to.

functions round1, round2 & round3 are very much the same code,
You could manage with just one function that alters only where needed.

functions won and lost also have a lot of duplicated code.

Cheers, I think I can see how to improve it. Very helpful thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code review of my rock paper scissors game Milan 0 2,015 May-25-2022, 06:59 AM
Last Post: Milan
  Rock paper Scissors Milan 2 2,785 Feb-01-2021, 03:42 PM
Last Post: Milan
  Rock Paper Scissors Game NectDz 5 3,307 May-31-2020, 12:40 PM
Last Post: NectDz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020