I am working on learning python (version 3), and as you are probably aware, one of the exercises is to create a game of rock paper scissors lizard spock (rpsls) I was required to start with the if statements featured below, then modify the code to include loops and use a random function to add a computer player. I have spent a few days adjusting the code and googling things, but I have not been able to fix the break in the loop. It gets stuck asking player one for input endlessly and never loads the comparison with player2 or finishes the round. I realize this is a messy way to code the game, but I would like to keep the formatting if possible.
import random
print("")
print("**** Welcome to Rock Paper Scissors ****")
print("")
inputOK = False
player2choices = ['rock', 'paper', 'scissors', 'lizard', 'spock']
while inputOK == False:
stringPlayer1 = input("Player 1, choose: rock, paper, scissors, lizard, or spock: ")
stringPlayer2 = random.choice(player2choices)
if stringPlayer1 == stringPlayer2:
print("Tie: Both players chose:" +
stringPlayer1)
elif stringPlayer1 == 'scissors' and stringPlayer2 == 'paper':
print("Player 1 wins: scissors cuts paper.")
elif stringPlayer1 == 'paper' and stringPlayer2 == 'rock':
print("Player 1 wins: paper covers rock.")
elif stringPlayer1 == 'rock' and stringPlayer2 == 'lizard':
print("Player 1 wins: rock crushes lizard.")
elif stringPlayer1 == 'lizard' and stringPlayer2 == 'spock':
print("Player 1 wins: lizard poisons spock.")
elif stringPlayer1 == 'spock' and stringPlayer2 == 'scissors':
print("Player 1 wins: Spock smashes scissors.")
elif stringPlayer1 == 'scissors' and stringPlayer2 == 'lizard':
print("Player 1 wins: scissors decapitates lizard.")
elif stringPlayer1 == 'lizard' and stringPlayer2 == 'paper':
print("Player 1 wins: lizard eats paper.")
elif stringPlayer1 == 'paper' and stringPlayer2 == 'spock':
print("Player 1 wins: paper disproves Spock.")
elif stringPlayer1 == 'spock' and stringPlayer2 == 'rock':
print("Player 1 wins: Spock vaporizes rock.")
elif stringPlayer1 == 'rock' and stringPlayer2 == 'scissors':
print("Player 1 wins: rock crushes scissors.")
elif stringPlayer1 == 'paper' and stringPlayer2 == 'scissors':
print("Player 2 wins: scissors cuts paper.")
elif stringPlayer1 == 'rock' and stringPlayer2 == 'paper':
print("Player 2 wins: paper covers rock.")
elif stringPlayer1 == 'lizard' and stringPlayer2 == 'rock':
print("Player 2 wins: rock crushes lizard.")
elif stringPlayer1 == 'spock' and stringPlayer2 == 'lizard':
print("Player 2 wins: lizard poisons spock.")
elif stringPlayer1 == 'scissors' and stringPlayer2 == 'spock':
print("Player 2 wins: Spock smashes scissors.")
elif stringPlayer1 == 'lizard' and stringPlayer2 == 'scissors':
print("Player 2 wins: scissors decapitates lizard.")
elif stringPlayer1 == 'paper' and stringPlayer2 == 'lizard':
print("Player 2 wins: lizard eats paper.")
elif stringPlayer1 == 'spock' and stringPlayer2 == 'paper':
print("Player 2 wins: paper disproves Spock.")
elif stringPlayer1 == 'rock' and stringPlayer2 == 'spock':
print("Player 2 wins: Spock vaporizes rock.")
elif stringPlayer1 == 'scissors' and stringPlayer2 == 'rock':
print("Player 2 wins: rock crushes scissors.")
else:
inputOK = False
print("Error: Not a valid choice.")
quit = input("Do you want to quit? ")
if quit.lower() == "y" or quit.lower() == "yes":
done = True
User will be stuck in infinite loop unless somewhere within the while loop the condition is changed by setting inputOK = True.
Before that you need to determine what will be the condition for valid input, such as user input being one of the valid choices (if stringPlayer1 in player1choices):
player2choices = ['rock', 'paper', 'scissors', 'lizard', 'spock']
player1choices = player2choices
while inputOK == False:
stringPlayer1 = input("Player 1, choose: rock, paper, scissors, lizard, or spock: ")
if stringPlayer1 in player1choices: # a condition for valid input?
inputOK = True
stringPlayer2 = random.choice(player2choices)
That should at least fix the broken loop and allow you to move along developing the game more.
The while loop will continue looping until your variable, inputOk, equals True. Your error is that inputOK is not being updated to True when the player wants to end the game. Perhaps you meant to set inputOk equal to True instead of done?
quit = input("Do you want to quit? ")
if quit.lower() == "y" or quit.lower() == "yes":
inputOk = True
Edit: Also as already mentioned, you must indent in python. Any code not indented under while statement will not loop.
Related
File "E:\P-L\Atom\RPS.py\RPS.py", line 34
if user_choice == "r":
IndentationError: unexpected indent
i'm new to code and i'm making a project
someone said the you learn better by doing projects
so here i am
i'm getting this on Atom
i've tried the same code on repl.it and it works
import random
comp_wins = 0
player_wins = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper or Scissors: ")
if user_choice in ["Rock", "rock", "r", "R"]:
user_choice = "r"
elif user_choice in ["Paper", "paper", "p", "P"]:
user_choice = "p"
elif user_choice in ["Scissors", "scissors", "s", "S"]:
user_choice = "s"
else:
print("I don't understand, try again.")
Choose_Option()
return user_choice
def Computer_Option():
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice = "r"
elif comp_choice == 2:
comp_choice = "p"
else:
comp_choice = "s"
return comp_choice
while True:
print("")
user_choice = Choose_Option()
comp_choice = Computer_Option()
print("")
if user_choice == "r":
if comp_choice == "r":
print("You chose rock. The computer chose rock. You tied.")
elif comp_choice == "p":
print("You chose rock. The computer chose paper. You lose.")
comp_wins += 1
elif comp_choice == "s":
print("You chose rock. The computer chose scissors. You win.")
player_wins += 1
elif user_choice == "p":
if comp_choice == "r":
print("You chose paper. The computer chose rock. You win.")
player_wins += 1
elif comp_choice == "p":
print("You chose paper. The computer chose paper. You tied.")
elif comp_choice == "s":
print("You chose paper. The computer chose scissors. You lose.")
comp_wins += 1
elif user_choice == "s":
if comp_choice == "r":
print("You chose scissors. The computer chose rock. You lose.")
comp_wins += 1
elif comp_choice == "p":
print("You chose scissors. The computer chose paper. You win.")
player_wins += 1
elif comp_choice == "s":
print("You chose scissors. The computer chose scissors. You tied.")
print("")
print("Player wins: " + str(player_wins))
print("Computer wins: " + str(comp_wins))
print("")
user_choice = input("Do you want to play again? (y/n)")
if user_choice in ["Y", "y", "yes", "Yes"]:
pass
elif user_choice in ["N", "n", "no", "No"]:
break
else:
break
Pretty sure the problem is with atom but i don't know what it is would love any help.
I am trying to make it so that when the user or the bot wins the round they get a point but at the end of the game, it seems to not add when one or the other gets it right, i tried the other way ('x = x + 1'), also please rate my code and tell me what i could've done better.
import random
print('Lets play Rock Paper Scissors')
for tries in range (1,4):
try:
user_guess = input('Rock Paper Scissors? ')
choices = ['Rock','Paper','Scissors']
user_point = 0 #To keep track of the points
bot_point = 0
bot_guess = random.choice(choices) #Will randomly pick from the list 'choices'
while user_guess not in choices:#if the user tries to put in anything other then the choices given
print('Please enter the choices above!')
user_guess = input('Rock Paper Scissors? ')
except ValueError:
print('Please choose from the choices above ') #Just in case user tries to put a different value type
user_guess = input('Rock Paper Scissors? ')
DEBUG = "The bot did " + bot_guess
print(DEBUG)
if user_guess == bot_guess:
print('Tie!')
elif user_guess == "Rock" and bot_guess == "Paper":
print('The bot earns a point!')
bot_point += 1
elif user_guess == 'Paper' and bot_guess == "Rock":
print('The user earns a point!')
user_point += 1
elif user_guess == 'Paper' and bot_guess == 'Scissors':
print('The bot earns a point')
bot_point += 1
elif user_guess == 'Scissors' and bot_guess == 'Paper':
print('The user earns a point')
user_point += 1
elif user_guess == 'Rock' and bot_guess == 'Scissors':
print('The user earns a point')
user_point += 1
elif user_guess == 'Scissors' and bot_guess == 'Rock':
print('The bot earns a point')
bot_point += 1
print('After ' + str(tries) + ' tries. ' + ' The score is')
print('The User: ' + str(user_point))
print('The Bot: ' + str(bot_point))
if user_point > bot_point:
print('THE USER IS THE WINNER!!!')
else:
print('THE BOT IS THE WINNER!!!')
You need to initialize user_point and bot_point before you start the for loop. The way it is, those are reset to zero each time through the loop.
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.
I am trying to create a program that lets the user choose to play rock, paper or scissors. Once they choose what they want to play and the amount of games that they want to play, they will be told if they won or lost.
I want to tally up the amount of times they won or lost, but instead it just prints after each round. Is there a way that I can make it print after the game is completed? I want to show the user the outcome overall at the end
from random import randint
def main():
games = int(input("How many games would you like to play?"))
while games > 0:
games -= 1
comp = computer()
choice = user()
print("You played", choice, "and the computer played", comp)
winner(comp, choice)
def computer():
comp = randint in range (0,3)
if comp == 0:
comp = 'rock'
elif comp == 1:
comp = 'paper'
elif comp == 2:
comp = 'scissors'
return comp
def user():
choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
if choice == 0:
choice = 'rock'
elif choice == 1:
choice = 'paper'
elif choice == 2:
choice = 'scissors'
else:
print("invalid input")
return choice
def winner(comp, choice):
tie = 0
win = 0
lose = 0
while True:
if choice == "rock" and comp == "rock":
result = 'tie'
tie += 1
break
elif choice == 'rock'and comp == 'scissors':
result = "you win"
win += 1
break
elif choice == 'rock' and comp == 'paper':
result = "you lose"
lose += 1
break
elif choice == 'paper' and comp == 'paper':
result = 'tie'
tie += 1
break
elif choice == 'paper' and comp == 'scissors':
result = 'you lose'
lose += 1
break
elif choice == 'paper' and comp == 'rock':
result = 'you win'
win =+ 1
break
elif choice == 'scissors' and comp == 'scissors':
result = 'tie'
tie += 1
break
elif choice == 'scissors' and comp == 'paper':
result = 'you win'
win += 1
break
elif choice == 'scissors' and comp == 'rock':
result = 'you lose'
lose +=1
break
else:
print("error")
break
print(result)
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")
main()
Declare globals for the win, lose and tie counters
randint(0, 2) is how you get random integer
It's += and not =+
There are couple of mistakes. I added a comment # CORRECTION at the respective lines.
from random import randint
tie = 0
win = 0
lose = 0
def main():
games = int(input("How many games would you like to play?"))
while games > 0:
games -= 1
comp = computer()
choice = user()
print("You played", choice, "and the computer played", comp)
winner(comp, choice)
def computer():
# CORRECTION: randint in range (0,3) always return FALSE. BELOW IS THE CORRECT WAY
comp = randint(0, 2)
if comp == 0:
comp = 'rock'
elif comp == 1:
comp = 'paper'
elif comp == 2:
comp = 'scissors'
return comp
def user():
choice = int(input("choose 0 for rock, 1 for paper, or 2 for scissors: "))
if choice == 0:
choice = 'rock'
elif choice == 1:
choice = 'paper'
elif choice == 2:
choice = 'scissors'
else:
print("invalid input")
return choice
def winner(comp, choice):
# CORRECTION: MAKE ALL THE COUNTERS GLOBAL
global tie
global win
global lose
while True:
if choice == "rock" and comp == "rock":
result = 'tie'
tie += 1
break
elif choice == 'rock'and comp == 'scissors':
result = "you win"
win += 1
break
elif choice == 'rock' and comp == 'paper':
result = "you lose"
lose += 1
break
elif choice == 'paper' and comp == 'paper':
result = 'tie'
tie += 1
break
elif choice == 'paper' and comp == 'scissors':
result = 'you lose'
lose += 1
break
elif choice == 'paper' and comp == 'rock':
result = 'you win'
# CORRECTION: ITS NOT =+ ITS +=
# win =+ 1
win += 1
break
elif choice == 'scissors' and comp == 'scissors':
result = 'tie'
tie += 1
break
elif choice == 'scissors' and comp == 'paper':
result = 'you win'
win += 1
break
elif choice == 'scissors' and comp == 'rock':
result = 'you lose'
lose +=1
break
else:
print("error")
break
print(result)
main()
print("you won", win,"times, and lost", lose,"times, and tied", tie,"times.")
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.')