5
\$\begingroup\$

I'm a beginner in Python and I've just finished a Rock, Paper, Scissors simulator in Python. It asks the user to enter paper, rock, or scissors and the program will generate a random number which will convert it to paper, rock or scissors. The program will then print the corresponding result. It also keeps tracks of wins/losses/ties until the user wants to stop playing then prints the prints the results back to the user.

import random as r
print("Welcome to my Rock-Paper-Scissor Game")
print("The rules are as follow:")
print("     Paper beats Rock")
print("     Rock beats Scissors")
print("     Scissors beat Paper")
print()
win=0
ties=0
loss=0
i=0
while i==0:
    while True:
        user=input("Do you pick Rock, Paper, or Scissors? ")
        if user not in ("Scissors","scissors","paper","Paper","Rock","rock"):
            print("Please enter Rock, Paper or Scissors. ")
            continue
        else:
            break
    computer=r.randint(1,3)
    if computer==1:
        computer="Paper"
    if computer==2:
        computer="Rock"
    if computer==3:
        computer="Scissors"
    if (user=="Paper" or user=="paper"):
        print("You choose paper")
        if computer=="Paper":
            print('\033[0;32m' + "It's a tie! The program also choose paper")
            ties=ties+1
        if computer=="Rock":
            print('\033[1;31m' + "You won! The program choose rock")
            win=win+1
        if computer=="Scissors":
            print('\033[1;36m' + "You lost! The program choose scissors")
            loss=loss+1

    if (user=="Rock" or user=="rock"):
        print("You choose Rock")
        if computer=="Rock":
            print('\033[0;32m' + "It's a tie! The program also choose rock")
            ties = ties + 1
        if computer=="Scissors":
            print('\033[1;31m' + "You won! The program choose scissors")
            win = win + 1
        if computer=="Paper":
            print('\33[1;36m' + "You lost! The program choose paper")
            loss = loss + 1

    if (user=="Scissors" or user=="scissors"):
        print("You choose Scissors")
        if computer=="Scissors":
            print('\033[0;32m' + "It's a tie! The program also choose scissors")
            ties = ties + 1
        if computer=="Paper":
            print('\033[1;31m' + "You won! The program choose paper")
            win = win + 1
        if computer=="Rock":
            print('\033[1;36m' + "You lost! The program choose rock")
            loss = loss + 1
    while True:
        question=input('\033[0;0m' + "Would you like to play again? ")
        if question not in ("Yes","yes","Y","y","no","No","n","N"):
            print('\033[;1m' + "Please only enter yes or no. ")
            continue
        else:
            break
    if question in ("Yes","y","Y","yes"):
        i=0
    if question in ("No","n","no","N"):
        i=1
print()
print("Your final results are as follows")
print("Number of wins:",win)
print("Number of ties:",ties)
print("Number of losses:",loss)
\$\endgroup\$
2
  • \$\begingroup\$ I would use functions like .upper() so that different capitalizations of the same string do not need to be checked. \$\endgroup\$
    – user127168
    Jun 18, 2017 at 3:55
  • \$\begingroup\$ I'm not a python programmer, so I won't post it as an answer, but I think you could actually use a dictionary to store who beats who. Checking tie would be (computer == user) and checking if the user won would be (dictionnary[user] == computer) or something like that. It will avoid writing all cases by hand \$\endgroup\$
    – Maliafo
    Jun 18, 2017 at 8:20

3 Answers 3

3
\$\begingroup\$

Yes/no handling

Don't repeat yourself or reinvent the wheel. There already exists a distutils.util.strtobool that does exactly what you're trying to accomplish:

Convert a string representation of truth to true (1) or false (0).

True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises ValueError if val is anything else.

You can convert the string to lowercase beforehand. Since strtobool raises a ValueError, your yes/no handling can be written succinctly like so:

while True:
    try:
        question=input('\033[0;0m' + "Would you like to play again? ")
        i = not strtobool(question)
        break
    except ValueError:
        print('\033[;1m' + "Please only enter yes or no. ")

Notice that strtobool returns 1 for truthy values, yet for some reason you have i == 0 represent "keep going", which is confusing. So I used not here, but I recommend either changing that or using proper bools here for readability.

\$\endgroup\$
3
\$\begingroup\$

Code style

Since you are a Python beginer, you can read the PEP 8 -- Style Guide for Python Code. This gide will teach you the best practices in Python coding style. You can use a modern IDE (like PyCharm) do develop in Python. It can found the coding style errors for you.

For instance:

Missing whitespace aroud operator, replace:

win=0
ties=0
loss=0
i=0

by:

win = 0
ties = 0
loss = 0
i = 0

Missing whitespace after ',', replace:

if user not in ("Scissors","scissors","paper","Paper","Rock","rock"):
    print("Please enter Rock, Paper or Scissors. ")
    continue

by:

if user not in ("Scissors", "scissors", "paper", "Paper", "Rock", "rock"):
    print("Please enter Rock, Paper or Scissors. ")
    continue

Remove redundant parentheses, replace:

if (user=="Paper" or user=="paper"):
    print("You choose paper")

by:

if user=="Paper" or user=="paper":
    print("You choose paper")

Simplify, be Pythonic

Simplify the comparison to True/False values, replace:

while i==0:
    ...

by:

while not i:
    ...

Use incrementation, replace:

ties = ties + 1

by:

ties += 1

Use str.lower (or str.upper) to simplify your test, replace:

if user not in ("Scissors", "scissors", "paper", "Paper", "Rock", "rock"):
    ...

by:

user = user.lower()
if user not in ("scissors", "paper", "rock"):
    ...

Choose the right name for your variables/functions/classes, the user variable is not an user but a thing that the user choose.

Be "positive" as often as your can, replace:

if user not in ("Scissors", "scissors", "paper", "Paper", "Rock", "rock"):
    print("Please enter Rock, Paper or Scissors. ")
    continue
else:
    break

by:

if user in ("Scissors", "scissors", "paper", "Paper", "Rock", "rock"):
    break
else:
    print("Please enter Rock, Paper or Scissors. ")
    continue

REVIEWED CODE:

while True:
    user_choice = input("Do you pick Rock, Paper, or Scissors? ")
    user_choice = user_choice.lower()
    if user_choice in ("scissors", "paper", "rock"):
        break
    print("Please enter Rock, Paper or Scissors.")

Note: the else is not necessary.

Use if/elif/else statement (like switch in other languages), that way, developers understand that all conditions are mutually exclusive. Again, turn the values to lower case (or uppercase) to match the user choice.

Replace:

if computer == 1:
    computer = "Paper"
if computer == 2:
    computer = "Rock"
if computer == 3:
    computer = "Scissors"

by:

if computer_choice == 1:
    computer_choice = "Paper"
elif computer_choice == 2:
    computer_choice = "Rock"
else:
    computer_choice = "Scissors"

Note: you can also use a dictionary to do the mapping between integers and things:

mapping = {1: "paper", 2: "rock", 3: "scissors"}
computer_choice = mapping[computer_choice]

Read the documentation and you'll find a function to choose a value in a collection of values.

REVIEWED CODE:

computer_choice = r.choice(["paper", "rock", "scissors"])

I think, you understand the idea…

\$\endgroup\$
2
\$\begingroup\$

This section

computer=r.randint(1,3)
if computer==1:
    computer="Paper"
if computer==2:
    computer="Rock"
if computer==3:
    computer="Scissors"

can be simplified with random.choice:

computer=r.choice(["Paper","Rock","Scissors"])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.