Friday, June 21, 2024

Rock Paper Scissors

**See 6/22 post for adjustments.**

Code can be tested at https://www.programiz.com/python-programming/online-compiler/ (Remove the 3 lines of "stuff" on the left on their site, or it might not run properly.)

First attempt at rock, paper, scissors in Python. I was inspired by this post, https://bsky.app/profile/coding.bsky.social/post/3kq7rtxldn42v

 

import random
#import numpy/itertools later (install, run pip, code later)

#a = 1
turnresults = []
choiceplayer = []
scoreplayer = 0
opponentresults = 0
#Do 128 later, 5 for now
while (len(turnresults)<5):
    #for a in range(1, 5):
    #Have to reindent if using for
    choice = input("Enter rock, paper, or scissors: ")
    opponent = (random.randint(1,3))

    if (choice == "rock") and (opponent == 2):
        print("Rock loses to paper \n")
        opponentresults = opponentresults + 1        
    if (choice == "rock") and (opponent == 3):
        print("Rock wins to scissors \n")
        scoreplayer = scoreplayer + 1        
    if (choice == "paper") and (opponent == 1):
        print("Paper wins to rock \n")
        scoreplayer = scoreplayer + 1  
    if (choice == "paper") and (opponent == 3):
        print("Paper loses to scissors \n")
        opponentresults = opponentresults + 1        
    if (choice == "scissors") and (opponent == 1):
        print("Scissors loses to rock \n")
        opponentresults = opponentresults + 1
    if (choice == "scissors") and (opponent == 2):
        print("Scissors wins to paper \n")
        scoreplayer = scoreplayer + 1

    #opponent = str(opponent)
    #choice = str(choice)

    if (choice == "rock" and opponent == 1) or (choice == "paper" and opponent == 2) or (choice == "scissors" and opponent == 3):        
        print("SAME!! Try again. \n")
        #print ("turnresults = ", turnresults)
        #print ("SAME / Turn results = ", turnresults)
        
    else:
        choiceplayer.append(choice)
        turnresults.append(opponent)
        ##print ("opponent's choices: ", turnresults)
        ##print ("your choices: ", choiceplayer)
        ##print (turnresults)
        print ("Score: YOU ", scoreplayer, " - THEM ", opponentresults)

if (scoreplayer > opponentresults):
    print ("***YOU WIN!!!***")
else:
    print ("***YOU LOSE!!!***")
               
        
    #print (len(turnresults))

    
    #if (choice == "rock" or choice == "paper" or choice == "scissors"):
        #print("choice is: " + choice)
        #opponent = (random.randint(1,3))
        #opponent = str(opponent)
        #turnresults.append(opponent)
        #print (turnresults)
    #else:
        #print("Type \"rock, paper, or scissors:\"")
        #choice = input()
        #turnresults.append(opponent)
        #print (turnresults)
        #a=a+1 #Doesn't work to get 5 turn results




#It doesn't complete 5 successful turn results when the for loop is used

No comments:

Post a Comment