Rock Paper Scissors Source Code

Posts 1–5 of 5 · Page 1 of 1
Rock Paper Scissors Source Code
#poloh
import random

userHand = input("rock, paper, or scissors")
computerHand = random.random()

if computerHand < .33:
computerHand = ("rock")
elif computerHand < .66:
computerHand = ("paper")
else:
computerHand = ("scissors")


def game(first,second):
if first == second:
print ("You chose",first)
print ("Computer chose", second)
print ("It's a tie!")
elif first == ("rock"):
if second == ("scissors"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")
elif first == ("paper"):
if second == ("rock"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")
elif first == ("scissors"):
if second == ("paper"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")

game(userHand,computerHand)
Quote Originally Posted by Poloh View Post
#poloh
import random

userHand = input("rock, paper, or scissors")
computerHand = random.random()

if computerHand < .33:
computerHand = ("rock")
elif computerHand < .66:
computerHand = ("paper")
else:
computerHand = ("scissors")


def game(first,second):
if first == second:
print ("You chose",first)
print ("Computer chose", second)
print ("It's a tie!")
elif first == ("rock"):
if second == ("scissors"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")
elif first == ("paper"):
if second == ("rock"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")
elif first == ("scissors"):
if second == ("paper"):
print ("You chose",first)
print ("Computer chose",second)
print ("You Win!")
else:
print ("You chose", first)
print ("Comuter chose", second)
print ("You lose.")

game(userHand,computerHand)
Put this inside the "CODE" tag in order to make it readable, moreover there is NO INDENTATION which is too bad in Python lol
Quote Originally Posted by K4LI View Post
Put this inside the "CODE" tag in order to make it readable, moreover there is NO INDENTATION which is too bad in Python lol
I was looking for that CODE function haha thanks!
Code:
#poloh
import random

userHand = input("rock, paper, or scissors")
computerHand = random.random()

if computerHand < .33:
    computerHand = ("rock")
elif computerHand < .66:
    computerHand = ("paper")
else:
    computerHand = ("scissors")


def game(first,second):
    if first == second:
        print ("You chose",first)
        print ("Computer chose", second)
        print ("It's a tie!")
    elif first == ("rock"):
        if second == ("scissors"):
            print ("You chose",first)
            print ("Computer chose",second)
            print ("You Win!")
        else:
            print ("You chose", first)
            print ("Comuter chose", second)
            print ("You lose.")
    elif first == ("paper"):
        if second == ("rock"):
            print ("You chose",first)
            print ("Computer chose",second)
            print ("You Win!")
        else:
            print ("You chose", first)
            print ("Comuter chose", second)
            print ("You lose.")
    elif first == ("scissors"):
        if second == ("paper"):
            print ("You chose",first)
            print ("Computer chose",second)
            print ("You Win!")
    else:
        print ("You chose", first)
        print ("Comuter chose", second)
        print ("You lose.")
game(userHand,computerHand)
Indented for easier reading.

Also, if you want to print on a new line, instead of doing three print statements, there should be some way to use a newline to put it all into one statement, idk how though.
Plus, since you're printing the users choice, and computers choice every time, you can print it above the if statements to remove a lot of repetition from your code.

Code:
print("You chose", first, "\nComputer chose", second)
if you care about using the newline over multiple print statements.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us