I'm new to Python. Need to create the game Rock, Paper, Scissors in Python 3 that prints out at the end of the game:
The number of rounds the computer has won.
The number of rounds the user has won.
The number of rounds that ended in a tie
The number of times the user selected each weapon (Rock, Paper, Scissors).
Note: Computer should select the weapon most likely to beat the user, based on the user’s previous choice of weapons, but its choice will not be displayed until later so the user doesn’t see it. For that computer keeps a count of how many times the user has selected each weapon (Rock, Paper or Scissors).
Example, if the user has selected Rock and Paper 3 times each and Scissors only 1 time, or if the user has selected each of the weapons an equal number of times, then there is no single weapon that has been used most frequently by the user; in this case the computer may select any of the weapons.
My code is next:
# Import random function
import random
# After the round ask the user if he/she would like to play the game again
def display_intro():
print("Welcome to Rock, Paper and Scissors Game!")
play_game = input("Would you like to play with me?\n"
"y/n").lower()
if play_game == "y" or play_game == "yes" or play_game == "Y":
print("Let\'s start playing!")
get_comp_choice()
elif play_game == "n" or play_game == "no" or play_game == "N":
print("Thanks for playing")
print("Computer won", compWinCt, "times.")"" \
print("User won", userWinCt, "times.")
print("Draw games count is: ",drawCt)
print("User picked Rock", userRockCt, "times.")
print("User picked Paper", userPaperCt, "times.")
print("User picked Scissors", userScissors, "times.")
quit()
else:
print("That's now a valid choice. Please try again!")
display_intro()
# define random choice pick
def get_random_choice():
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice = "r"
elif comp_choice == 2:
comp_choice = "p"
elif comp_choice == 3:
comp_choice = "s"
return comp_choice
# define computer choice base on what user has chosen in previous rounds
def get_comp_choice():
if userRockCt == 0 and userPaperCt == 0 and userScissorsCt == 0:
return get_random_choice()
else:
if (userRockCt > userPaperCt) and (userRockCt > userScissorsCt):
return comp_choice == "r"
elif (userPaperCt > userRockCt) and (userPaperCt > userScissorsCt):
return comp_choice == "p"
elif (userScissorsCt > userPaperCt) and (userScissorsCt > userRockCt):
return comp_choice == "s"
def main():
while True:
# set count for a zero
userRockCt = 0
userPaperCt = 0
userScissorsCt = 0
compWinCt = 0
userWinCt = 0
drawCt = 0
# Get computer choice
comp_choice = get_comp_choice()
# Get user choice from the input
user_choice = input("Please enter from the menue: \n"
"r for rock,"
"p for paper"
"s for scissors"
"q to quit")
# Check who has won /lost or if the game is draw
# Get the count for how many times user picke Rock, Paper or Scissors
# Get the count for how many times won computer and user and the count for the draw
while comp_choice == "r":
if user_choice == "r":
print("This is a DRAW. You chose Rock and Computer chose Rock.")
userRockCt += 1
drawCt += 1
elif user_choice == "p":
print("YOU WIN. You chose Paper and Computer chose Rock. Paper covers Rock.")
userPaperCt += 1
userWinCt += 1
elif user_choice == "s":
print("YOU LOSE. You chose Scissors and Computer chose Rock. Rock breaks Scissors.")
userScissorsCt += 1
compWinCt += 1
else:
print("Try again")
display_intro()
while comp_choice == "p":
if user_choice == "r":
print("YOU LOSE. You chose Rock and Computer chose Paper. Paper covers Rock.")
userRockCt += 1
compWinCt += 1
elif user_choice == "p":
print("This is a DRAW. You chose Rock and Computer chose Rock.")
userPaperCt += 1
drawCt += 1
elif user_choice == "s":
print("YOU WIN. You chose Scissors and Computer chose Paper. Scissors cut Paper.")
userScissorsCt += 1
userWinCt += 1
display_intro()
while comp_choice == "s":
if user_choice == "r":
print("YOU WIN. You chose Rock and Computer chose Scissors. Rock breaks Scissors.")
userRockCt += 1
userWinCt += 1
elif user_choice == "p":
print("YOU LOSE. You chose Paper and Computer chose Scissors. Scissors cut Paper.")
userPaperCt += 1
compWinCt += 1
elif user_choice == "s":
print("This is a DRAW. You chose Scissors and Computer chose Scissors.")
userScissorsCt += 1
drawCt += 1
display_intro()
main()
I don't know if I am misplacing the order of define function or missing something. When I run the code I get this error.
Traceback (most recent call last):
File "C:/Users/.../Test", line 123, in <module>
main()
File "C:/Users/.../Test", line 64, in main
comp_choice = get_comp_choice()
File "C:/Users/.../Test", line 43, in get_comp_choice
if userRockCt == 0 and userPaperCt == 0 and userScissorsCt == 0:
NameError: name 'userRockCt' is not defined
Process finished with exit code 1
And just looking at the code I can tell that the same issue will be with userPaperCt, userScissorsCt at get_comp_choice
and at def display_intro() with compWinCt, userWinCt and DrawCt
I've seen some examples of using global but I'm trying to avoid using it.