I went for my second GPN coding session last week.
It was my second time visiting University of Western Australia to participate in the Girls Programming Network (GPN). GPN is where girls can learn coding with the help of from tutors. I found my second visit much more fun as I was able to make my own Scissors, Paper and Rock game in python.
## code created by Ahla (10/03/24)
## Version: 2.01
## code for playing rock paper scissors with computer
## using a import random code for the computer_move
import random
## Define a function returning the move as a string
def moveExpansion(move):
if move == "s":
return "Scissors"
elif move == "p":
return "Paper"
else:
return "Rock"
## adding a new dictionary called results to determine the winner of each game
results = {
("p", "r"): "human",
("p", "s"): "computer",
("p", "p"): "draw",
("s", "p"): "human",
("s", "r"): "computer",
("s", "s"): "draw",
("r", "p"): "computer",
("r", "s"): "human",
("r", "r"): "draw",
}
## Keep the scores by initializing int variables
human_score_counter = 0
computer_score_counter = 0
## print rules:
print("Welcome to Human vs. Computer Game in scissors, paper, rock!\nMoves: Choose scissors (s), paper (p), or rock (r) by typing in your selection.\nRules:\nScissors can cut paper,\nPaper covers rock,\nRock crushes scissors.\nGood luck!\n")
## define player_name as a variable to store the user's name
player_name = input("Enter your name: ")
## Ask the user to enter the number of trials; only numbers are accepted
## ######## Infinite loop begins ########
while True:
number_of_games = input ("How many times do you want to play? ")
try:
## adding an integer statement for numbers
number_of_games= int (number_of_games)
## break out of the infinite loop
break
except ValueError:
print("This is not a number! Please enter a valid number.")
## ######## Infinite loop ends ########
## Begin the game
## ######## For loop begins for the number of trials entered ########
for i in range(number_of_games):
## ######## Infinite loop begins ########
while True:
## using variables for human-move and computer_move to recors the moves
## Ask human for their move
human_move = input(player_name+", what is your move: s (scissors), p (paper) or r (rock)? ")
if (human_move[0]=="S")or(human_move[0]=="s")or(human_move[0]=="P")or(human_move[0]=="p")or(human_move[0]=="R")or(human_move[0]=="r"):
## Convert the string to lower case for results dictionary
human_move = human_move.lower()
## break out of the infinite loop
break
else:
print("This is not a move! Please enter a valid move.")
## ######## Infinite loop ends ########
## Let's ask the computer for a random move and simplify the move entries as single alphabets
## computer_move = random.choice(["scissors", "paper", "rock"])
computer_move = random.choice(["s", "p", "r"])
## In print, Use comma as a delimiter to prefix a space
## In print Use plus as a delimiter to avoid space
print(player_name,"played:", moveExpansion(human_move[0]))
print("Computer played:", moveExpansion(computer_move))
winner = (results[(human_move[0], computer_move)])
## using if, elif and else statements for announcing the game winner
if winner == "draw":
print ("It's a draw!\n")
elif winner == "human":
print (player_name + " won the game!\n")
human_score_counter+=1
else:
print ("Computer won the game!\n")
computer_score_counter = computer_score_counter+1
## ######## For loop ends ########
## Final tournament scores
print ("=============================\nGAME OVER!")
print ("Human won",human_score_counter,"games.")
print ("Computer won",computer_score_counter,"games.")
## Determining who is the final winner of the tournament
if human_score_counter == computer_score_counter:
print ("The Tournament is a draw!")
elif human_score_counter > computer_score_counter:
print (player_name + " won the Tournament!")
else:
print ("Computer won the Tournament!")
print ("=============================")
During the GPN session I used the software platform Replit to write the code and later used Python IDLE.
I learnt a lot of new python functions like dictionary, if elif and else statements, import random and more that was useful for my coding.
The GPN session helped me learn more coding and I would like to thank the GPN tutors who helped me Sheree Pudney, Michelle, Sanjana, Ella, Jyothi, Luara, Shuray and others. The tutors present helped me when I got stuck during the program.
GPN also provides workbooks, presentation slides and cheat sheets to help you with coding. It can be accessed from: https://www.girlsprogramming.network/perth-workshop