RPS- Creating a tally with end result in Python - python

My teacher wants us to create a basic rock paper scissors game that loops until the user ends the game, at which point the program will add up all the wins/losses/ties (of the player) and display it. Here is what I have so far, but I cannot figure out how to create that running tally in the background that will spit out the calculation at the end. (The bottom part of the win/loss/tie product is written by my teacher. It must print this way.)
def main():
import random
Wins = 0
Ties = 0
Losses = 0
while True:
user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")
play_again = input("Play again? (y/n): ")
if play_again.lower() != "y":
print("Good Game!")
print("Wins \t Ties \t Losses")
print("---- \t ---- \t ------")
print(wins, "\t", ties , "\t", losses)import random

Where you are printing out wether the user has tied, lost or won you can simply increment the related value.
The text may not be displaying together due to the text not being the same size, add more /t to the strings and it should line up.

Related

Rock, Paper, Scissors. Code won't read user input that's lowercase

I made a rock paper scissors game (code below). Is there a simpler way of making the code able to read both uppercase and lowercase inputs? So far, it only reads uppercase (i.e. Rock, Paper, Scissors). If the input is lowercase (i.e. rock, paper, scissors), the game won't tell you if you've won or not.
import random
import time
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n")
computerChoice = (random.choice(options))
print("Rock...")
time.sleep(1)
print("Paper...")
time.sleep(1)
print("Scissors...")
time.sleep(1)
print("Shoot! \n")
time.sleep(1)
print("You chose " + yourMove)
print("The computer chose " + computerChoice + "\n")
if yourMove == "Rock" and computerChoice == "Paper":
print("You lose!")
elif yourMove == "Paper" and computerChoice == "Rock":
print("You win!")
if yourMove == "Scissors" and computerChoice == "Rock":
print("You lose!")
elif yourMove == "Rock" and computerChoice == "Scissors":
print("You win!")
if yourMove == "Paper" and computerChoice == "Scissors":
print("You lose!")
elif yourMove == "Scissors" and computerChoice == "Paper":
print("You win!")
if yourMove == computerChoice:
print("It's a draw!")
I would recommend that if you are going to work with strings in a list, leave everything in lowercase.
When capturing the value desired by the user, you could use:
value = input("[Text] ").lower().strip()
Python is a case sensitive language, so
"Paper" != "paper" or " paper " != " paper"
python has many methods to work with strings.
your program can use many of them and work.
here are some solutions.
1- use lower() to make user input all lowercase and your options should be lower cased too.
options = ["rock", "paper", "scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").lower()
2- use upper() to make user input all uppercase and your options should be upper case.
options = ["ROCK", "PAPER", "SCISSORS"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").upper()
3- or you can keep your current options and just use capitalize() to make the first letter uppercase.
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").capitalize()

Function not taking changed variable [duplicate]

This question already has answers here:
Why does assigning to my global variables not work in Python?
(6 answers)
Closed 8 months ago.
I'm trying to create a rock, paper, scissors game using functions but when I run the game it doesn't show who won. The function is called "Game()". That is the code:
import random
playerChoice=0
computerChoice=0
computerChoices=["Rock","Paper","Scissors"]
def Game():
def Rock():
if playerChoice=="Rock":
if computerChoice=="Rock":
print("Tie")
if computerChoice=="Paper":
print("You Lost")
if computerChoice=="Scissors":
print("You Won")
else:
Paper()
def Paper():
if playerChoice=="Paper":
if computerChoice=="Paper":
print("Tie")
if computerChoice=="Scissors":
print("You Lost")
if computerChoice=="Rock":
print("You Won")
else:
Scissors()
def Scissors():
if computerChoice=="Scissors":
print("Tie")
if computerChoice=="Rock":
print("You Lost")
if computerChoice=="Paper":
print("You Won")
Rock()
def Play():
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
print(f"Computer Picked {computerChoice}")
Game()
Play()
If you examine the choices right before you call Rock() in Game, you'll see that playerChoice and computerChoice are still set to 0. This is because when you do
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
it doesn't actually change the values of the global variables playerChoice or computerChoice. This is called shadowing.
What you can do is accept the choices as arguments:
def Game(playerChoice, computerChoice):
# ...
def Play():
playerChoice=input("Do you pick rock, paper or scissors? \n").capitalize()
computerChoice=random.choice(computerChoices)
print(f"Computer Picked {computerChoice}")
Game(playerChoice, computerChoice)
Also, you have an indentation error in Paper(), it should be this:
def Paper():
if playerChoice=="Paper":
if computerChoice=="Paper":
print("Tie")
if computerChoice=="Scissors":
print("You Lost")
if computerChoice=="Rock":
print("You Won")
else:
Scissors()
Your if statements are a bit messed up. Try using elif more.Try studying this.
import random
user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = random.choice(possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")

Always receive 'You Lose' message in RPS against computer

For my exercise, I have to get the computer to play against the user in RPS. However, after both the computer and I input our action, the user will always receive a 'You Lose'.
import random
def get_computer_move():
"""Deterministic 'random' move chooser"""
options = ['rock', 'paper', 'scissors']
return options[random.randint(0, 2)]
def game_result(move1, move2):
"""game_result *exactly* as defined in question one"""
options = ["rock", "paper", "scissors"]
if move1 == move2:
return 0
elif move1 == "paper" and move2 == "rock":
return 1
elif move1 == "rock" and move2 == "scissors":
return 1
elif move1 == "scissors" and move2 == "paper":
return 1
else:
return 2
def main():
"""Runs a single game of PSR"""
print("Let's Play Paper, Scissors, Rock!")
user1 = input("Player one move: ").lower()
computer1 = print("The computer plays " + get_computer_move())
game_result(computer1, user1)
if user1 == computer1:
return print("It's a draw")
elif user1 == "paper" and computer1 == "rock":
return print("You win")
elif user1 == "rock" and computer1 == "scissors":
return print("You win")
elif user1 == "scissors" and computer1 == "paper":
return print("You win")
else:
return print("You lose")
main()
Here is my code although it is quite messy.
What I want to happen is:
Let's Play Paper, Scissors, Rock!
Player one move: ROCK
The computer plays rock
It's a draw
But it will always come out as:
Let's Play Paper, Scissors, Rock!
Player one move: ROCK
The computer plays rock
You lose
Help would be appreciated.
print returns none, you want to save the computers move and then print that separately
computer1 = get_computer_move()
print("The computer plays", computer1)

How can I Implement Win/loss statistics in Rock Paper Scissors Game?

I want to display the current statistics of the game after every match
import random
while True:
user_action = input("Enter a choice: (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
comp_action = random.choice(possible_actions)
wins = 0
losses = 0
ties = 0
print(f"\nYou chose {user_action}, computer chose {comp_action}.\n")
if user_action == comp_action:
print(f"Both players selected {user_action}. It's a tie!")
ties +=1
elif user_action == "rock":
if comp_action == "scissors":
print("Rock beats scissors. You win!")
wins += 1
else:
print("Paper beats Rock. You lose! ")
losses +=1
elif user_action == "paper":
if comp_action == "rock":
print("Paper beats rock. You win!")
wins += 1
else:
print("Scissors beats paper! You lose.")
losses += 1
elif user_action == "scissors":
if comp_action == "paper":
print("Scissors beats paper! You win!")
wins += 1
else:
print("Rock beats scissors! You lose.")
losses +=1
print(f"won {wins / sum((wins, losses, ties))}%, {ties} ties")
again = input("Want to play again? (y/n): ")
if again.lower() != "y":
break
I want to show the computer's wins, the user's wins, and the amount of ties along with their win/loss percentages.
Player wins: 0 (0.0%)
Computer wins: 1 (100.0%)
Any help is greatly appreciated.
make 3 counters for the game endings:
wins = losses = ties = 0
and increase them every time it happens like so:
print("Rock beats scissors. You win!")
wins += 1
at the end print them:
print(f"won {wins / sum((wins, losses, ties))}%, {ties} ties")

If condition within game running as if true, when it should be false. (Python) [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
When I run this code and input "r" "p" or "s" the first if condition within the function runs as though it is true. My goal is for the else statement to run when the user inputs a valid answer, such as "s" "p" or "r". Tried debugging, calling the global choice variable, not sure what to do here. Help would be appreciatd. This is Python 3.7.
possible_options = ["rock", "paper", "scissors"]
choice = input("Enter r for rock, p for paper, and s for scissors: ")
def game():
global choice
print(choice)
if choice != "r" or choice != "p" or choice != "s":
choice = input("Invalid choice. Must be r, p, or s: ")
game()
else:
selection = choice
if selection == "r":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("It was a tie!")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("Paper covers rock! You lose.")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("Rock smashes scissors! You win!")
elif selection == "p":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("Paper covers rock! You win!")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("It was a tie!")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("Scissors cut paper! You lose.")
elif selection == "s":
if random.choice(possible_options) == "rock":
print("The computer chose rock!")
print("Rock smashes scissors! You lose.")
elif random.choice(possible_options) == "paper":
print("The computer chose paper!")
print("Scissors cut paper! You win!")
elif random.choice(possible_options) == "scissors":
print("The computer chose scissors!")
print("It was a tie!")
game()
Replace
if choice != "r" or choice != "p" or choice != "s":
with
if choice != "r" and choice != "p" and choice != "s":

Categories