Python Forum
Rock Paper Scissors Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock Paper Scissors Game
#1
I just want to receive some feedback on my code on things I can improve such as if my code can be cleaner or things I can add, anything helps!

import time
import random

rps = ("R","P","S")

def stop():
    while True:
        for i in range(1):
            break
def score():
    while True:
        time.sleep(1)
        print("\nThe Score\nWins -",counter_W,"\nLoses -",counter_L, "\nTies -", c_T)
        break

def restart():
    global rounds
    time.sleep(.5)
    rounds -= user_R
    while True:
        restart = input("\nType 'C' to Continue\nType 'Q' to Quit: ")
        restart = restart.upper()
        if restart == 'C':
            time.sleep(.5)
            game()
        elif restart == 'Q':
            print("\nEnding game...")
            stop()

# Counters (Tie, Win, Loss)

c_T = 0
counter_W = 0
counter_L = 0

rounds = 0

def game():
    global c_T, counter_W, counter_L, rounds, user_R
    user_R = int(input("\nType the amount of rounds you would like to play: "))
    while True:
        rps_R = random.choice(rps)
        time.sleep(.5)
        user = input("\nType 'R' for Rock\nType 'P' for Paper\nType 'S' for Scissors: ")
        user = user.upper()
        # Tie
        if user == 'R' and rps_R == 'R' or user == 'P' and rps_R == 'P' or user == 'S' and rps_R == 'S':
            c_T += 1
            rounds += 1
            print("\nUser has tied", c_T)
        # User Wins Rock
        if user == 'R' and rps_R == 'S':
            counter_W += 1
            rounds += 1
            print("\nUser has won:", counter_W)
        # User Wins Paper
        elif user == 'P' and rps_R == 'R':
            counter_W += 1
            rounds += 1
            print("\nUser has won:", counter_W)
        # User Wins Scissors
        elif user == 'S' and rps_R == 'P':
            counter_W += 1
            rounds += 1
            print("\nUser has won:", counter_W)
        # -----------------------------------
        # User loses Rock
        if user == 'R' and rps_R == 'P':
            counter_L += 1
            rounds += 1
            print("\nUser has lost:", counter_L)
        # User Wins Paper
        elif user == 'P' and rps_R == 'S':
            counter_L += 1
            rounds += 1
            print("\nUser has lost:", counter_L)
        # User Wins Scissors
        elif user == 'S' and rps_R == 'R':
            counter_L += 1
            rounds += 1
            print("\nUser has lost:", counter_L)

        if rounds == user_R:
            score()
            restart()

def intro():
    print("Rock Paper Scissors Game")
    time.sleep(.1)
    print("  _____  _____   _____  ")
    print(" |  __ \|  __ \ / ____|")
    time.sleep(.1)
    print(" | |__) | |__) | (___  ")
    print(" |  _  /|  ___/ \___ \ ")
    time.sleep(.1)
    print(" | | \ \| |     ____) | ")
    print(" |_|  \_\_|    |_____/ ")
    time.sleep(.1)
    print(" Coded by NectDz\n")

def main():
    intro()
    time.sleep(1)
    while True:
        start = input("Type 'Start' to begin: ")
        start = start.upper()
        if start == 'START':
            game()
        else :
            print("\nDid not understand restarting...\n")
            time.sleep(2)
            main()

main()
Reply
#2
Well, you could also add a statement that shows what the computer chose - but otherwise the game is great
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
That stop function (line 6) is very odd. Why do you need to write that instead of just a break on line 28?
Reply
#4
(May-30-2020, 07:21 AM)ndc85430 Wrote: That stop function (line 6) is very odd. Why do you need to write that instead of just a break on line 28?
Since my code has multiple while loops if I were to just
 break 
the program would still keep on running so creating the
 stop 
function helps to deal with that.
Reply
#5
i added some try and except but you can add more such as IOError etc
def game():
    try:
        global c_T, counter_W, counter_L, rounds, user_R
        user_R = int(input("\nType the amount of rounds you would like to play: "))
        while True:
            rps_R = random.choice(rps)
            time.sleep(.5)
            user = input("\nType 'R' for Rock\nType 'P' for Paper\nType 'S' for Scissors: ")
            user = user.upper()
            # Tie
            if user == 'R' and rps_R == 'R' or user == 'P' and rps_R == 'P' or user == 'S' and rps_R == 'S':
                c_T += 1
                rounds += 1
                print("\nUser has tied", c_T)
            # User Wins Rock
            if user == 'R' and rps_R == 'S':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # User Wins Paper
            elif user == 'P' and rps_R == 'R':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # User Wins Scissors
            elif user == 'S' and rps_R == 'P':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # -----------------------------------
            # User loses Rock
            if user == 'R' and rps_R == 'P':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)
            # User Wins Paper
            elif user == 'P' and rps_R == 'S':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)
            # User Wins Scissors
            elif user == 'S' and rps_R == 'R':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)

            if rounds == user_R:
                score()
                restart()
    except KeyboardInterrupt as err:
        print(err)


def main():
    intro()
    time.sleep(1)
    while True:
        try:
            start = input("Type 'Start' to begin: ")
            start = start.upper()
            if start == 'START':
                game()
            else:
                print("\nDid not understand restarting...\n")
                time.sleep(2)
                main()
        except KeyboardInterrupt as err:
            print(err)
main()
Reply
#6
(May-30-2020, 06:22 PM)Calli Wrote: i added some try and except but you can add more such as IOError etc
def game():
    try:
        global c_T, counter_W, counter_L, rounds, user_R
        user_R = int(input("\nType the amount of rounds you would like to play: "))
        while True:
            rps_R = random.choice(rps)
            time.sleep(.5)
            user = input("\nType 'R' for Rock\nType 'P' for Paper\nType 'S' for Scissors: ")
            user = user.upper()
            # Tie
            if user == 'R' and rps_R == 'R' or user == 'P' and rps_R == 'P' or user == 'S' and rps_R == 'S':
                c_T += 1
                rounds += 1
                print("\nUser has tied", c_T)
            # User Wins Rock
            if user == 'R' and rps_R == 'S':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # User Wins Paper
            elif user == 'P' and rps_R == 'R':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # User Wins Scissors
            elif user == 'S' and rps_R == 'P':
                counter_W += 1
                rounds += 1
                print("\nUser has won:", counter_W)
            # -----------------------------------
            # User loses Rock
            if user == 'R' and rps_R == 'P':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)
            # User Wins Paper
            elif user == 'P' and rps_R == 'S':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)
            # User Wins Scissors
            elif user == 'S' and rps_R == 'R':
                counter_L += 1
                rounds += 1
                print("\nUser has lost:", counter_L)

            if rounds == user_R:
                score()
                restart()
    except KeyboardInterrupt as err:
        print(err)


def main():
    intro()
    time.sleep(1)
    while True:
        try:
            start = input("Type 'Start' to begin: ")
            start = start.upper()
            if start == 'START':
                game()
            else:
                print("\nDid not understand restarting...\n")
                time.sleep(2)
                main()
        except KeyboardInterrupt as err:
            print(err)
main()
Nice! After finishing the rock paper scissor I myself have started using try and except and I will look up more on IOError, thanks for your feedback and addition to the code!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code review of my rock paper scissors game Milan 0 397 May-25-2022, 06:59 AM
Last Post: Milan
  Rock paper Scissors Milan 2 1,692 Feb-01-2021, 03:42 PM
Last Post: Milan
  2nd Project: Rock, Paper, Scissors MiNigle 3 1,602 Jun-05-2020, 12:50 PM
Last Post: MiNigle

Forum Jump:

User Panel Messages

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