Dec-02-2018, 09:36 PM
Hello, I'm learning Python at college but it's an online course and I've never met the instructor or a class mate yet. Everything is read your book and turn in a lab each week. All online so I have no one to actually speak to for help. I have a Rock Paper Scissors game that is due on Dec 3 that has certain criteria that I'm having a hard time with. Can someone help me? The game has to accept input from the user as lowercase 'r' 's' 'p' or 'q' for quit. Anything else needs to error and go back to choose r p s. The game needs to display info such as round # and wins, loses and ties and increment as such. The game also needs to keep a list or dictionary of players choices made so it can learn and predict what you favor to make it harder. I have most of it but I need help on the dictionary part and maybe error checking . Here is my code so far:
import os import random import time import sys #globalA variable #0 is a placeholder and will not be used choices = {'r': 'rock', 'p': 'paper', 's': 'scissors', 'q': 'quit'} player_wins = 0 computer_wins = 0 tie_count = 0 round_number = 1 keep_playing = True # sets cls() to clear screen def cls(): os.system('cls' if os.name == 'nt' else 'clear') # function to display stats def stats(): print("Current Statistics:") print("Player Wins: {}".format(player_wins)) print("Computer Wins: {}".format(computer_wins)) print("Tied Games: {}\n".format(tie_count)) # function to check outcome def game_outcome(player, computer): #this makes the variables global, else you'll get error global player_wins, tie_count, computer_wins if computer == player: print("It's a tie!\n\n") tie_count += 1 # incraments tie # checks all possible win conditions for player. and if met, declares player a winner. If not, declares compute the winner. elif (player == "rock" and computer == "scissors") or (player == "paper" and computer == "rock") or (player == "scissors" and computer == "paper"): print("Player wins\n\n") player_wins += 1 # incraments player's wins else: print("Computer wins!\n\n") computer_wins += 1 # incraments computer's wins # clears screen cls() print("Let's play Rock Paper Scissors!") # 3-second time out before clearing and asking for input time.sleep(3) while keep_playing == True: # make computer choice random from defined list. Only selects a range of 1-3 ignoring the "0" placeholder # this is because the user selects a number, instead of typing the weapon, and that number pulls the weapon from the list computer = random.choice(['rock', 'paper', 'scissors']) cls() # prints starting of round and shows stats #print("+++++++++++++[Starting Round {}]+++++++++++++\n".format(round_number)) #stats() # ask for player input player = input("What is your choice?\n(r) Rock\n(p) Paper\n(s) Scissors?\n\nEnter the letter before the weapon of choice:") player = {'r': 'rock', 'p': 'paper', 's': 'scissors', 'q': 'quit'} print("\n\nThe player's choice: [{}]\n".format(player)) print("The computer's choice: [{}]\n\n".format(computer)) game_outcome(player, computer) round_number += 1 stats() # ask if player wants to play again. If not, stop play_again = input("Press 'enter' to continue or 'q' to quit. ") if play_again.lower() == 'q': cls() break stats() print("Thanks for playing!\n")