python rock paper scissors [duplicate] - python

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 7 years ago.
Im new to programming and trying to build a simple rock paper scissors program in python 2.7. in my function i have 2 main if statements
rules = raw_input("Before we being playing would you like to hear the rules first? ")
if rules.lower() == "yes":
print """
Scissors cuts Paper
Paper covers Rock
Rock crushes Scissors"""
and the second
choice = raw_input("please enter your choice? (Must be either rock, paper or scissors) ")
computer = random.choice(["rock", "paper", "scissors"])
if choice == "rock" or "paper" or "scissors":
if choice == computer :
print "Its a tie !"
elif choice == "rock" and computer == "scissors":
print "Rock beats scissors you win!."
elif choice == "rock" and computer == "paper":
print "Paper beats rock you loose !."
elif choice == "paper" and computer == "scissors":
print "Scissors beats paper you loose !."
elif choice == "paper" and computer == "rock":
print "Paper beats rock you win !."
elif choice == "scissors" and computer == "paper":
print "Scissors beats paper you win !."
elif choice == "scissors" and computer == "rock":
print "Rock beats scissors you loose !."
else :
print "Invalid Entry Please try again."
individually both bits of code work as they should but when i try and put them together in one function the first if statement asking about rules works but then quits before the second if statement which has the main functionality of the program. Ive tried indenting the second bit of code within the first if statement but it doesn't seem to work
I was wondering if there is anyway of making these 2 snips of code work in a simple function ? or should i create a class with these 2 functions ?
also if anyone has any tips on how to make my progam better please let me know. Thanks for any help in advance.
heres the full code
import random
def rock_paper_scissors_spock():
rules = raw_input("Before we being playing would you like to hear the rules first? ")
if rules.lower() == "yes":
print """
Scissors cuts Paper
Paper covers Rock
Rock crushes Scissors"""
choice = raw_input("please enter your choice? (Must be either rock, paper or scissors) ")
computer = random.choice(["rock", "paper", "scissors"])
if choice == "rock" or "paper" or "scissors":
if choice == computer :
print "Its a tie !":
elif choice == "rock" and computer == "scissors":
print "Rock beats scissors you win!."
elif choice == "rock" and computer == "paper":
print "Paper beats rock you loose !."
elif choice == "paper" and computer == "scissors":
print "Scissors beats paper you loose !."
elif choice == "paper" and computer == "rock":
print "Paper beats rock you win !."
elif choice == "scissors" and computer == "paper":
print "Scissors beats paper you win !."
elif choice == "scissors" and computer == "rock":
print "Rock beats scissors you loose !."
else :
print "Invalid Entry PLease try again."
rock_paper_scissors_spock()

You said if choice == "rock" or "paper" or "scissors":, but Python does not connect the choice == to all of the choices. You could put parentheses around choice == "rock", and it would do the same thing. Change it to if choice in ("rock", "paper", "scissors")

Related

RPS- Creating a tally with end result in 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.

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":

Rock Paper Scissors Not Printing Out Results

I am trying to create a Rock Paper Scissors program against a computer but running into some issues.
I have made two methods, one for the computer's choice and the user's choice. For the computer, it randomly generates 0,1,2 and selects rock, paper or scissors from an array which I declared as a local variable, rps. When I try to run the game with the game_play() method, I am able to enter in input but don't get an output of the result between the player and the computer.
class RockPaperScissors():
global rps
rps = ['rock', 'paper','scissors']
def computer_choice(self): #computer randomly picks rock paper or scissors
x = random.randint(0,2)
w = rps[x]
return w
#print(rps[x])
def player_choice(self): #does the player choose rock paper or scissors
x = True
while x:
choice = (input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: "))
if choice.lower() == 'rock':
return 'rock'
elif choice.lower() == 'paper':
return 'paper'
elif choice.lower() == 'scissors':
return 'scissors'
else:
("please enter in rock, paper or scissors")
def game_play(self):
rock = RockPaperScissors()
user = rock.player_choice()
comp= rock.computer_choice()
if comp == 'rock' and user == 'paper':
return "the player wins!"
elif comp == 'rock' and user == 'scissors':
return "the computer wins!"
elif comp == 'paper' and user == 'rock':
return "the computer wins!"
elif comp == 'paper' and user == 'scissors':
return "the player wins!"
elif comp == 'scissors' and user == 'paper':
return"the computer wins!"
elif comp == 'scissors' and user == 'rock':
return "the player wins"
I am trying to test it this way:
rock = RockPaperScissors()
rock.game_play()
If you are running it as a script instead of everything in the Python interpreter. You must explicitly print the value.
One possible solution is:
rock = RockPaperScissors()
print(rock.game_play())
Directly answering your question: you are not "printing" the game, which would be...
print(rock.game_play())
However, there's some other improvements I would do.
Avoid global variables so that you can provide greater clarity to
the code. Maybe it's a better idea to define rps inside the function
computer_choice() instead of having it as a global variable.
You can simplify player_choice() as:
x = input("Player- would you like rock, paper, or scissors? enter rock,paper, or scissors?: ").lower()
while x != 'rock' and x != 'paper' and x != 'scissors':
choice = (input("Player- would you like rock, paper, or scissors? enter rock, paper, or scissors?: "))
x = input("please enter in rock, paper or scissors").lower()
return x
Possibly, your code wasn't copied correctly, but everything below the first line (class RockPaperScissors:) is missing one indent.
Finally, another possible solution is to print the game's result inside function game_play() instead of returning a string.
Good luck and keep learning.

Rock, Paper And Scissors Program (Upper And Lower Case Problem)

I Was Practicing Programs Of Python And Then I Made This Rock, Paper, Scissors Program. And The Program Is Running Perfectly But The Problem Is Like This:
I Chose "Rock, Paper, Scissors" Like This Than It Runs.
But If I Enter Rock, Paper, Scissors In Any Other Way It Prints The Else Statment i.e., Invalid Choice.
I Want To Use Upper/Lower Case But How And Where.
import random
user = input("""Tell Me What Do You Choose:
~Rock
~Paper
~Scissors.
-> I Choose : """)
choose = ["Rock", "Paper", "Scissors"]
computer = random.choice(choose)
print(f"Computer Chose {computer}.")
#Function To Compare Choices
def compare(user, computer):
if user == computer:
return("It's A Tie :(....")
elif user == "Rock":
if computer == "Scissors":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Scissors":
if computer == "Paper":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Paper":
if computer == "Rock":
return("User Wins!")
else:
return("Computer Wins!")
else:
return("User Choose Out Of The Options!")
exit()
print(compare(user, computer))
just transform your input into lowercase with the lower() method
user = user.lower()
that makes sCiSsOrS into scissors
you also need to adjust your if statements to be all lowercase
elif user == "Rock": --> elif user == "rock":

incorrect outputs in a python rock paper scissors game

i have been trying to make a python rock paper scissors game with my limited python knowledge but good understanding of computer/ programming logic. here is my code:
def Main():
from random import randint
global computerChoice
global userChoice
print "Ok, let's play 3 rounds."
for i in range(3):
userChoice = raw_input("Rock, paper, or scissors? ")
computerChoice = randint(1,3)
if userChoice == "Rock" or "rock":
userChoice = 1
choiceCompare()
elif userChoice == "Paper" or "paper":
userChoice = 2
choiceCompare()
else:
userChoice = 3
choiceCompare()
def choiceCompare():
global userChoice
global computerChoice
if userChoice == computerChoice:
print "I chose the same thing! It's a draw!"
elif userChoice != computerChoice:
if userChoice == 1:
if computerChoice == 2:
print "Yes! I chose paper, you lost!"
elif computerChoice == 3:
print "Dang. I chose scissors, you win."
elif userChoice == 2:
if computerChoice == 1:
print "Man. I chose rock, you win."
elif computerChoice == 3:
print "I chose scissors, you loose!"
else:
if computerChoice == 1:
print "Ha! I chose rock, you loose."
elif computerChoice == 2:
print "Aww, man! I chose paper, you win."
if __name__ == "__main__":
Main()
when i run it, i get completely wrong outputs like this:
Ok, let's play 3 rounds.
Rock, paper, or scissors? paper
I chose the same thing! It's a draw!
Rock, paper, or scissors? paper
I chose the same thing! It's a draw!
Rock, paper, or scissors? paper
Dang. I chose scissors, you win.
or this:
Ok, let's play 3 rounds.
Rock, paper, or scissors? scissors
Yes! I chose paper, you lost!
Rock, paper, or scissors? scissors
Yes! I chose paper, you lost!
Rock, paper, or scissors? scissors
Dang. I chose scissors, you win.
however, rock seems to be working fine:
Ok, let's play 3 rounds.
Rock, paper, or scissors? rock
Dang. I chose scissors, you win.
Rock, paper, or scissors? rock
I chose the same thing! It's a draw!
Rock, paper, or scissors? rock
Yes! I chose paper, you lost!
can anyone tell me what they think is wrong? thanks in advance, Liam.
P.S. i think there is something wrong with my choiceCompare function in the nested if statements. I'm not quite sure what though. also, ignore the automatic coloring in the bits where i copied the text from the program running, as they are not actually python code but text from the program.
Your problem(s) start in these lines:
userChoice == "Rock" or "rock"
Either use userChoice.lower(), or use userChoice in ("Rock", "rock"). That'll help initially, and that's just at first glance.
To expand. The issue here is that you're comparing userChoice == "Rock", which is either True or False, then you're saying or "rock" which evaluates to True on its own (any non-empty string is True). See also: this answer on string truth values.
userChoice == "Rock" or "rock"
This line will always evaluate to true because it is like (userChoice == "Rock") or "rock"
Try this instead:
userChoice == "Rock" or userChoice == "rock"
See this link for documentation of boolean operators: https://docs.python.org/3.1/library/stdtypes.html

Categories