Python 3 ; Rock Paper Scissors - Just Keeps Choosing One Outcome - python-3.x

Writing a little RPS game and I ran into a problem.
1) When I run the function to play the game, it always returns as a "Tie"
even if I choose a losing variable ex. cpu = rock, player = scissors
I'm kind of stumped on this. I usually lurk the stackoverflow forum when I'm having troubles, but I haven't encountered someone that has the same problem (even with the plethora of RPS questions on here).
Now, I'm not asking for anybody to write and test it 100% for me, but rather just show me the error spot and I'll go troubleshoot.
Thanks!
def showRules():
print("********** Rock, Paper, Scissors **********")
print("Rules: Each player chooses either Rock, Paper, or Scissors.")
print(" The winner is determined by the following rules:")
print(" Scissors cuts Paper -> Scissors wins")
print(" Paper covers Rock -> Paper Wins")
print(" Rock smashes Scissors -> Rock Wins")
print("*******************************************")
def getCPUChoice():
choices = ["rock", "paper", "scissors"]
from random import randint
randNum = randint(0,2)
cpuchoice = choices[randNum]
print(cpuchoice)
return
def getUserChoice():
choices = ["rock", "paper", "scissors"]
userchoice = input('Please choose either rock, paper or scissors:').lower()
print(userchoice)
return
def declareWinner(user, computer):
if user == computer:
print("Tie!!")
elif user == "rock" and computer == "paper":
print("You lose!")
elif user == "paper" and computer == "scissors":
print("You lose!")
elif user == "scissors" and computer == "rock":
print("You lose!")
def playGame():
showRules()
computer = getCPUChoice()
user = getUserChoice()
declareWinner(user, computer)

In getCPUChoice and getUserChoice, you are printing the choices instead of returning them. Change the returns at the end of those functions to
return cpuchoice
and
return userchoice
respectively.

Related

My rock, paper, scissor game does give no outputs after inputting my move. What is wrong with my code?

I can't get the hang of what I'm doing wrong here. Whenever I run the code the program just stops after taking input. I don't understand why does it not proceed by entering the functions.
Any help will be appreciated
import random
def Main():
g_options = ["rock", "paper", "scissor"]
rand_choice = random.choice(g_options)
print(rand_choice)
per_choice = input("Rock, Paper, Scissor?").islower()
# valid = False
while per_choice == rand_choice:
per_choice = input("Oh same answers. Let's do it again. Rock, paper, Scissor?").islower
if per_choice == "paper":
Paper_Choice(per_choice)
elif per_choice == "rock":
Rock_Choice(per_choice)
elif per_choice == "scissor":
Scissor_Choice(per_choice)
def Rock_Choice(X):
if X == "scissor": # This is if computers choice is rock
return "you lose"
elif X == "paper":
return "you win"
def Paper_Choice (X):
if X == "rock": # This is if computers choice is paper
return "you lose"
elif X == "scissor":
return "you win"
def Scissor_Choice(X):
if X == "rock": # This is if computers choice is scissor
return "you win"
elif X == "paper":
return "you lose"
Main()
Your input variable ist set to the string.is_lower function -you do not call it so it does not even tests if the whole input is lower case or not:
per_choice = input("Rock, Paper, Scissor?").islower
You probably ment to use
per_choice = input("Rock, Paper, Scissor?").lower()
to make it lower case.
Your check methods only use if: ... elif:... no else: so you get no output at all.
# X is the "function_adress" of str.is_lower function
def Rock_Choice(X):
if X == "scissor": # This is if computers choice is rock
return "you lose"
elif X == "paper":
return "you win"
def Paper_Choice (X):
if X == "rock": # This is if computers choice is paper
return "you lose"
elif X == "scissor":
return "you win"
def Scissor_Choice(X):
if X == "rock": # This is if computers choice is scissor
return "you win"
elif X == "paper":
return "you lose"
You could have easily debugged that using print statements to veryfy what X is inputted into your methods:
def Rock_Choice(X):
print(X)
if X == "scissor": # This is if computers choice is rock
return "you lose"
elif X == "paper":
return "you win"
or next to where you get the values:
while per_choice == rand_choice:
per_choice = input("Oh same answers. Let's do it again. Rock, paper, Scissor?").islower
print(per_choice)
etc.

RPS code not runs but will not return any output

I am trying to create a rock, paper, scissors game in PyCharm using Python 3.6.3 but it will not return any output upon execution and I have not received any code errors in my IDE.
import random
class RPS:
rock = 1
paper = 2
scissors = 3
def __init__(self, choice):
self.choice = choice
def play(self):
print("Enter: \n1 for Rock, \n2 for Paper, \nor 3 for Scissors")
choice = int(input("Enter in a number between 1 and 3: "))
while (choice != 1 or choice != 2 or choice != 3):
print("You have selected an invalid choice!")
print("Enter: \n1 for Rock, \n2 for Paper, \nor 3 for Scissors")
choice = int(input("Enter in a number between 1 and 3: "))
print("You have selected", choice)
computer = random.randint(1, 3)
if computer == 1:
print("The computer chose rock")
if computer == 2:
print("The computer chose paper")
if computer == 3:
print("The computer chose scissors")
if choice > computer:
print("You win!")
elif choice < computer:
print("You lose!")
elif choice == computer:
print("It is a tie!")
play_again = input('Do you want to play again[yes/no]? ')
if play_again.lower() == 'yes':
self.play()
if play_again.lower() == 'no':
print("Thanks for playing! \nBYE!")
as jasonharper said, you aren't instancing the class and running it's play method.
to do that, add to the bottom of the file:
game = RPS()
game.play()
There are some other issues too, but I'll let you try to solve those yourself.

Python 3: modified yahtzee/ 3 of a kind, 4 of a kind, 5 of a kind or nothing

I'm writing a modified Yahtzee game in Python 3 called '5 Dice' where the user only wins if they roll a 3, 4, or 5 of a kind. I am using a list for the dice roll but I am having trouble comparing the random list values and now it doesn't even want to work. Please help!
import time
import os
import random
number_of_dice = 5
rolls = []
def dice_roll():
os.system("clear")
print("Welcome to 5 Dice!")
raw_input("Press ENTER to roll")
for q in range(number_of_dice):
rolls.append(random.int(1,6))
rolls.sort()
time.sleep(1)
print(*rolls)
if rolls[0] == rolls[2]:
print("You rolled a three of a kind!")
try_again()
if rolls[0] == rolls[3]:
print("You rolled a four of a kind!")
try_again()
if rolls[0] == rolls[4]:
print("You rolled a five of a kind!")
try_again()
def try_again():
choice = input("Would you like to play again? Y/N: ")
if choice == "Y" or choice == "y":
dice_roll()
if choice == "N" or choice == "n":
quit()
else:
print("Please type Y or N")
I can currently see two problems with your code:
1.
Firstly, your code is indented at the beginning for some reason. This results in an indentation error, which could be why your program is not even working. If you move all the code at the start backwards, it should work.
2.
When you are comparing the dice, you say: if rolls[0] == rolls[2]. This doesn't mean three are the same, because you are only comparing two of the dice! It should be more like: if rolls[0] == rolls[1] == rolls[2]. If you change this, your game should work.
I hope this helps.

Python 3 Rock, Paper, Scissors game NameError: name 'computer_choice_rock' is not defined

I am making a rock, paper, scissors game for a programming class. This is where I got and then PowerShell spits out that error. I don't understand what is wrong (I am a beginning Python programmer). My programming teacher is not much help and prefers the "Figure it out" approach to learning. I am genuinely stuck at this point. Any help is appreciated, thank you!
import random
def rps():
computer_choice = random.randint(1,3)
if computer_choice == 1:
comuter_choice_rock()
elif computer_choice == 2:
comuter_choice_paper()
else:
comuter_choice_scissors()
def computer_choice_rock():
user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
if user_choice == "1":
print ("It's a Tie!")
try_again()
if user_choice == "2":
print ("You Win! Paper covers Rock!")
try_again()
if user_choice == "3":
print ("I Win and You Lose! Rock crushes Scissors!")
try_again()
else:
print ("Please type in 1, 2, or 3")
computer_choice_rock()
def computer_choice_paper():
user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
if user_choice == "1":
print ("I Win and You Lose! Paper covers Rock!")
try_again()
if user_choice == "2":
print ("It's a Tie!")
try_again()
if user_choice == "3":
print ("You Win! Scissors cut Paper!")
try_again()
else:
print ("Please type in 1, 2, or 3")
computer_choice_paper()
def computer_choice_paper():
user_choice = input("1 for Rock, 2 for Paper, 3 for Scissors: ")
if user_choice == ("1"):
print ("You Win! Rock crushes Scissors")
try_again()
if user_choice == "2":
print ("I Win! Scissors cut Paper!")
try_again()
if user_choice == "3":
print ("It's a Tie!")
try_again()
else:
print ("Please type in 1, 2, or 3")
computer_choice_paper()
def try_again():
choice = input("Would you like to play again? Y/N: ")
if choice == "Y" or choice == "y" or choice == "Yes" or choice == "yes":
rps()
elif choice == "n" or choice == "N" or choice == "No" or choice == "no":
print ("Thanks for Playing!")
quit()
else:
print ("Please type Y or N")
try_again()
rps()
You have a typo in you code
if computer_choice == 1:
comuter_choice_rock()
elif computer_choice == 2:
comuter_choice_paper()
else:
comuter_choice_scissors()
Comuter
Your code can be simplified to an extreme degree. See the following example program. To replace either of the players with a NPC, set player_1 or player_2 with random.choice(priority). If you want to, you could even have the computer play against itself.
priority = dict(rock='scissors', paper='rock', scissors='paper')
player_1 = input('Player 1? ')
player_2 = input('Player 2? ')
if player_1 not in priority or player_2 not in priority:
print('This is not a valid object selection.')
elif player_1 == player_2:
print('Tie.')
elif priority[player_1] == player_2:
print('Player 1 wins.')
else:
print('Player 2 wins.')
You could also adjust your game so people can play RPSSL instead. The code is only slightly different but shows how to implement the slightly more complicated game. Computer play can be implemented in the same way as mentioned for the previous example.
priority = dict(scissors={'paper', 'lizard'},
paper={'rock', 'spock'},
rock={'lizard', 'scissors'},
lizard={'spock', 'paper'},
spock={'scissors', 'rock'})
player_1 = input('Player 1? ')
player_2 = input('Player 2? ')
if player_1 not in priority or player_2 not in priority:
print('This is not a valid object selection.')
elif player_1 == player_2:
print('Tie.')
elif player_2 in priority[player_1]:
print('Player 1 wins.')
else:
print('Player 2 wins.')

How do I fix an error: expected an indented block

I keep getting this error message that says expected an indent block.
The shell highlights the bold spot as the error. Any suggestions on how I fix it?
I am just a beginner so any and all help would be appreciated! Thank you in advance!
from random import *
s = choice( [ 'rock', 'paper', 'scissors' ])
def rps( ):
""" runs a game of Rock Paper Scissors between the program and the user by returning the user's choice and the program's choice and signaling a winner
input: the user's choice which is one of three responses in the game Rock-Paper- Scissors - all are a string of letters
"""
print ('rps( )')
print ('Welcome to a game of Rock Paper Scissors!')
print ('I just made my choice.')
print ('I promise no cheating this time!')
r = input('Now you choose rock, paper, or scissors!')
print
if r == 'paper':
print ('You chose paper.')
if s == 'scissors':
print ('I chose scissors. Haha, I win fair and square!')
elif s == 'rock':
print ("I chose rock. Maybe I'll have better luck next time...")
elif s == 'paper':
print ('We need a rematch!')
elif r == 'scissors':
print ('You chose scissors.')
if s == 'rock':
print ('I chose rock. Haha, I win fair and square!')
elif s == 'paper':
print ("I chose paper. Maybe I'll have better luck next time...")
elif s == 'scissors':
print ('We need a rematch!')
elif r =='rock':
print ('You chose rock.')
if s == 'paper':
print ('I chose paper. Haha, I win fair and square!')
elif s == 'scissors':
print ("I chose scissors. Maybe I'll have better luck next time...")
elif s == 'rock':
print ('We need a rematch!'
else:
print ("Don't you know the rules? Choose rock, paper or scissors!")
I think I fixed it!
from random import *
s = choice( [ 'rock', 'paper', 'scissors' ])
def rps( ):
""" runs a game of Rock Paper Scissors between the program and the user by returning the user's choice and the program's choice and signaling a winner
Technically this function has no inputs and no return value; the function is an interaction between the user and the program
"""
print ('Welcome to a game of Rock Paper Scissors!')
print ('I just made my choice.')
print ('I promise no cheating this time!')
r = input('Now you choose rock, paper, or scissors!')
if r == 'paper':
print ('You chose paper.')
if s == 'scissors':
print ('I chose scissors. Haha, I win fair and square!')
elif s == 'rock':
print ("I chose rock. Maybe I'll have better luck next time...")
elif s == 'paper':
print ('We need a rematch!')
elif r == 'scissors':
print ('You chose scissors.')
if s == 'rock':
print ('I chose rock. Haha, I win fair and square!')
elif s == 'paper':
print ("I chose paper. Maybe I'll have better luck next time...")
elif s == 'scissors':
print ('We need a rematch!')
elif r =='rock':
print ('You chose rock.')
if s == 'paper':
print ('I chose paper. Haha, I win fair and square!')
elif s == 'scissors':
print ("I chose scissors. Maybe I'll have better luck next time...")
elif s == 'rock':
print ('We need a rematch!')
else:
print ("Don't you know the rules? Choose rock, paper or scissors!")
Ok... all of this is really confused.
First of all, there is nothing inside the function rps() except for a comment. This is how you define a function:
def hi_im_a_function():
hi im inside the function
hi, im not inside the function.
hi_im_a_function()
^calling the function
In fact, that whole function appears to be obsolete. I don't know what you were trying to accomplish with that print('rps()') thing, but it isn't doing anything. I suggest you drop it.
The error is coming from that random floating print on the line right before the error. The error is landing on the if r == paper line because that is where the compiler first feels the effects of the error you set in the line before it.
As before, I don't know what you were trying to do there, but that statement has no purpose there. Delete it.
Fix those errors, and it should work like a charm.

Resources