Python Forum

Full Version: Rock Paper Scissors Game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
Well, you could also add a statement that shows what the computer chose - but otherwise the game is great
That stop function (line 6) is very odd. Why do you need to write that instead of just a break on line 28?
(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.
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()
(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!