Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import choice
- plays = ["Rock", "Paper", "Scissors"]
- #outcome function
- def outcome(winner, pscore, cscore):
- print("\n")
- if winner == "Player":
- print(winner + " score!")
- pscore += 1
- return pscore
- elif winner == "Computer":
- print(winner + " score!")
- cscore +=1
- return cscore
- else:
- print("Draw!")
- print("\n")
- #main game function
- def rock_paper_scissors():
- #set up player scores and loop exit condition
- quit = False
- p_score = 0
- c_score = 0
- while quit == False:
- #try running game code; if inout is incorrect format, remind user
- try:
- #collect user choice input and randomly choose computer move using choice mthod from random library
- user_choice = int(input("Type the number for your choice of action: Rock[0], Paper[1], Scissors[2] or Quit[other]?"))
- computer_play = choice(plays)
- #If the user chose a game move by typing 0-2, print their corresponding choice and the computer's too
- print("\n")
- if user_choice >= 0 and user_choice <= 2:
- user_play = plays[user_choice]
- print("Player chooses: " + user_play)
- print("Computer chooses: " + computer_play)
- #if the user chose 3 they wish to end the game; make user_play Quit so no conditions match
- else:
- print("Player chooses: Quit")
- quit = True
- user_play = "Quit"
- #Draw conditions
- if computer_play == user_play:
- outcome("draw", p_score, c_score)
- #Rock conditions
- elif user_play == "Rock" and computer_play == "Scissors":
- p_score = outcome("Player", p_score, c_score)
- #Paper conditions
- elif user_play == "Paper" and computer_play == "Rock":
- p_score = outcome("Player", p_score, c_score)
- #Scissors conditions
- elif user_play == "Scissors" and computer_play == "paper":
- p_score = outcome("Player", p_score, c_score)
- #quit condition
- elif user_play == "Quit":
- print("Thanks for playing!")
- quit = True
- #Computer win condition
- else:
- c_score = outcome("Computer", p_score, c_score)
- #print new line and return player and computer scores
- print("\n")
- print("Player score: " + str(p_score), "Computer score: " + str(c_score))
- print("\n")
- #if input is not an integer, remind user
- except ValueError:
- print("\n")
- print("Incorrect input format; input must be an integer!")
- print("\n")
- #calling game function
- rock_paper_scissors()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement