implementing while loop in rock, paper, scissors - python

I'm very new to coding so don't mind the baby code. i managed to set up a simple RPS game, however, for my assignment i need to use an infinite loop to make the program ask for the input again if the user makes an error. for example, if the enter 'roc' or 'scisor'. I cannot figure out where to input the while loop to make it ask for the input again. here is what I have so far:
player=input('rock. paper. or scissors?:')
computer_options= ['rock', 'paper', 'scissors']
computer=random.choice(computer_options)
if player==computer:
print('draw')
elif player=='rock':
if computer=='scissors':
print('you win')
else:
print('you lose')
elif player=='paper':
if computer=='scissors':
print('you lose')
else:
print('you win')
while player != computer:
print('you picked the wrong option')
break

Put the while on the outside, and put your break in an if block inside the loop:
while True:
player=input('rock. paper. or scissors?:')
computer_options= ['rock', 'paper', 'scissors']
computer=random.choice(computer_options)
if player==computer:
print('draw')
elif player=='rock':
if computer=='scissors':
print('you win')
else:
print('you lose')
elif player=='paper':
if computer=='scissors':
print('you lose')
else:
print('you win')
if player != computer:
print('you picked the wrong option')
break

You can use this code
computer_options= ['rock', 'paper', 'scissors']
computer=random.choice(computer_options)
while True:
player=input('rock. paper. or scissors?:')
if player.lower() == "rock" or player.lower() == "paper" or player.lower() == "scissors":
if player==computer:
print('draw')
elif player=='rock':
if computer=='scissors':
print('you win')
else:
print('you lose')
elif player=='paper':
if computer=='scissors':
print('you lose')
else:
print('you win')
else:
print("you did a mistake, select again between rock, paper or scissors")
continue
I created an infinite while loop. You need to insert the variable player inside this loop to play the game, then I inserted the condition if player.lower() == "rock" or player.lower() == "paper" or player.lower() == "scissors": meaning that if you wrote correctly the word, the game is starting, otherwise you will receive the message "you did a mistake, select again between rock, paper or scissors". Then, the word continue starts the loop again and you will have to put a new value to the variable player.
Tell me if you need any more help, for exemple adding the possibility to leave the game or anything else

Related

Rock paper scissors game how to make it infinite

How can I make it so the game is infinite? and is there a way to simplify this code?
I have tried to work around but can't seem to figure it out.
# A rock paper scissors game.
import random
Move1=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2=["r","p","s"]
while Move1 != "q":
if Move1 == "r" or "p" or "s" or "q":
# print(random.choice(Move2))
Move2=random.choice(Move2)
if Move1=="r" and Move2=="s":
print("You've won")
break
elif Move2=="p":
print("You lost!")
break
elif Move2=="r":
print("You went even!")
break
if Move1=="p" and Move2=="s":
print("You lost!")
break
elif Move2=="p":
print("You went even!")
break
elif Move2=="r":
print("You won!")
break
if Move1=="s" and Move2=="s":
print("You went even!")
break
elif Move2=="p":
print("You won!")
break
elif Move2=="r":
print("You lost!")
break
else:
print("You've quit the game!")
exit()
Tried to remove break
Move the request for input inside the loop. Use while True: around the loop to make it infinite. Remove all the break statements after reporting the winners and losers.
Don't use the same variable Move2 for the list of moves and the computer's move. That will prevent making a computer choice on the 2nd round.
# A rock paper scissors game.
import random
allowed_moves=["r","p","s"]
while True:
player_move=input("Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
if player_move in allowed_moves:
Move2=random.choice(allowed_moves)
if player_move=="r" and Move2=="s":
print("You've won")
elif Move2=="p":
print("You lost!")
elif Move2=="r":
print("You went even!")
if player_move=="p" and Move2=="s":
print("You lost!")
elif Move2=="p":
print("You went even!")
elif Move2=="r":
print("You won!")
if player_move=="s" and Move2=="s":
print("You went even!")
elif Move2=="p":
print("You won!")
elif Move2=="r":
print("You lost!")
else if player_move == 'q':
print("You've quit the game!")
break
else:
print("That's not a valid move!")
You have to re-evaluate the input at the end of the while loop. Or you put it into the while condition. So you could do while (Move1 := input(...)) != "q".
Also your first if check is always true because of the or "p". You would have to do or Move1 == "p" or Move1 == "s" or Move1 == "q"
You could simplify it with if Move1 in {"r", "p", "s", "q"}
Abstracting some of the game logic to a function is a start in the direction of simplifying. But there are other ways to simplify. Here's an example of it along with what I think you mean by making the game infinite.
def play(move1, move2):
# the logic
print("you win")
# more logic
is_playing = True
while is_playing:
Move1 = input(
"Enter your move: (r)ock (p)aper (s)cissors or (q)uit: ").lower()
Move2 = ["r", "p", "s"]
if Move1 == 'q':
print("You've quit the game!")
is_playing = False
break
play(Move1, Move2)
Let me know if that helps or not

What do I need to do next in this Rock-Paper-Scissors code?

I'm stuck on the last bit of this code. I don't know what to do next.
I need the game to pick a random option out of the listed options so they can battle the users pick.
Could someone point me in the right direction?
# Rock paper Scissors Game
# Question 1
print("Want to play Rock Paper Scissors??")
question1=input("Y/N: ").lower()
if question1=='y':
print('Perfect but first')
elif question1== 'n':
print('Sorry but your here already')
else: print('error')
# Question 2
print('Do you know how to play??')
question2=input("Y/N: ").lower()
if question2=='y':
print('Ok lets get started!')
elif question2=='n':
print('Pick 1 of the 3 options to battle my option')
print('Rock beats scissors')
print("Paper beats rock")
print('Scissors beats paper')
else:
print('error')
# Display game options
options = ['Rock', 'Paper', "Scissors"]
def random_choose(options):
pick = input('Take your pick: ')
return pick, random.choose(options)
Well, for this question a while loop would help the game be more dynamic, allowing the player to until the player opt-out of the game. I also integrated parts of your code into this revised one. Hope this helps
import random as r
game = True
print("Want to play Rock Paper Scissors??")
question1=input("Y/N: ").lower()
options = ['Rock', 'Paper', "Scissors"]
if question1=='y':
print('Perfect but first')
print('Do you know how to play??')
question2=input("Y/N: ").lower()
if question2=='y':
print('Ok lets get started!')
elif question2=='n':
print('Pick 1 of the 3 options to battle my option')
print('Rock beats scissors')
print("Paper beats rock")
print('Scissors beats paper')
while game:
pick = input('Take your pick(rock,paper or scissors): ')
if pick.capitalize() in options:
Ai = r.choice(options)
if (pick.lower()=="scissors"and Ai.lower()=="rock") or (pick.lower()=="paper" and Ai.lower()=="scissors") or (pick.lower()=="rock" and Ai.lower()=="paper"): #this is the rules for Ai vs user input(in this case Ai wins)
print("you lose, "+ Ai +" beats "+ pick )
elif (pick.lower()=="rock"and Ai.lower()=="scissors") or (pick.lower()=="scissors" and Ai.lower()=="paper") or (pick.lower()=="paper" and Ai.lower()=="rock"): #this is the rules for Ai vs user input(in this case player wins)
print("you win, "+ pick +" beats "+ Ai )
else: # in rare cases where both are the same
print("it's a tie, "+pick +" draws with " +Ai)
elif pick.lower() not in options: #if the user input is not found
print("option was not found")
cont = input("do you want to continue [Y/N]: ")
if cont.lower() =="n": #opting out(user quitting statement)
game = False
else:
continue
elif question1== 'n':
print('Alright good bye')

Do not want to let Computer play his turn once Player enters invalid choice. How to achieve?

I'm a beginner and writing a code for "Rock Paper Scissors" game. I don't want to run this game(code) over and over again, hence, used while loop. Now, at the "else:" step when any invalid choice is typed by the Player, Computer plays it's turn too after which "Invalid choice. Play your turn again!" is displayed.
I want when a Player types any invalid choice, the computer should not play it's turn and we get "Invalid choice. Play your turn again!" displayed, keeping the game running.
Please check my code and point me to the issue. Please explain with the correction. Thanks in advance!
print("Welcome to the famous Rock Paper Scissors Game. \n")
Choices = ["Rock", "Paper", "Scissors"]
while(True):
Player = input("Your turn: ")
Computer = random.choice(Choices)
print(f"Computer's turn: {Computer} \n")
if Player == Computer:
print("That's a tie, try again! \n")
elif Player == "Rock" and Computer == "Scissors":
print("You Won!!! \n")
elif Player == "Rock" and Computer == "Paper":
print("Computer won! \n")
elif Player == "Paper" and Computer == "Scissors":
print("Computer won! \n")
elif Player == "Paper" and Computer == "Rock":
print("You Won!!! \n")
elif Player == "Scissors" and Computer == "Paper":
print("You Won!!! \n")
elif Player == "Scissors" and Computer == "Rock":
print("Computer won! \n")
else:
print("Invalid choice. Play your turn again! \n")
You can check if the input is valid before the computer plays, and ask again if it is invalid using continue -
Choices = ["Rock", "Paper", "Scissors"]
while(True):
Player = input("Your turn: ")
if Player not in Choices: # If it is not in Choices list above
print("Invalid choice. Play your turn again! \n")
continue # This will re run the while loop again.
# If Player gives valid input, then continues this
Computer = random.choice(Choices)
print(f"Computer's turn: {Computer} \n")
# The next lines ....
Also check out - Break, Continue and Pass

Python Rock Paper Scissors List Choices

Instructions: Write a program to allow the user to play Rock, Paper, Scissors against the computer. The program will be menu driven (see details below). The user can continue to play the game over and over by choosing to play the game in the menu. A game consists of a single battle: win, lose, or tie. For a single battle the user makes their move and then the computer move is displayed followed by a description of who won that battle. Do not break a tie.
Once the user chooses to quit, the program then displays the win/loss/tie record for all the games just played
The user will enter the details in the following order during gameplay:
Menu choice (1 integer, 1 to play a game, 2 to quit)
Player's move (1 string, 'Rock', 'Paper', or 'Scissors')
My problem is that I'm not sure how to make the computer choose from my list and accept it. I keep getting this...1
import random
def main():
"""
Runs a game of Rock, Paper, Scissors. Asks the user for input and plays against the computer.
"""
#print display message
print('Lets play Rock, Paper, Scissors!')
moves_list = ['Rock', 'Paper', 'Scissors']
#a place to keep track of scores
tied = 0
computer = 0
player = 0
#create a variable to control loop
play_again = 1
comp_choice = process_comp()
player_choice = process_player()
winner = determine_winner(player_choice, comp_choice)
#while loop for multiple games
while play_again == 1:
comp_choice = process_comp()
player_choice = process_player()
if comp_choice == 'Rock' or comp_choice == 'rock':
print('Computer chooses rock.')
elif comp_choice == 'Paper' or comp_choice == 'paper':
print('Computer chooses paper.')
else:
print('Computer chooses scissors.')
#call the determine winner function
winner = determine_winner(player_choice, comp_choice)
#check who won the game and add 1 to the correct winner
if winner == 'computer':
computer += 1
elif winner == 'player':
player += 1
else:
tied += 1
#ask user if they want to play again
play_again = input('Would you like to play again? (Enter 1 for yes, 2 for no): ')
#display number of games that were won
print()
print('There were', tied, 'tied games.')
print('The player won', player, 'games.')
print('The computer won', computer,'games.')
#define the computer process function
def process_comp():
#make computer choose a move from list
choosen = random.choice(moves_list)
return choosen
#make sure user enters correct option.
def process_player():
player_choice = int(input('Rock, Paper, Scissors? (Enter 2 if you want to quit):'))
#if the player enters 2, quit the game
if player_choice == 2:
print('Thank you for playing!')
play_again = 2
#check if user is inputting correct selections
while player_choice != 'Rock' or player_choice != 'rock' and player_choice != 'Paper' or player_choice != 'paper' and player_choice != 'Scissors' or player_choice != 'scissors' and player_choice != 2:
print('Error! Please enter an option that was given.')
player_choice = int(input('Rock, Paper, Scissors? (Enter 2 if you want to quit):'))
return player_choice
#define the determine winner function.
def determine_winner(player_choice, comp_choice):
#setup computer selections
if comp_choice == 'Rock' or comp_choice == 'rock':
if player_choice == 'Paper' or player_choice == 'paper':
print('You win!')
winner = 'player'
elif player_choice == 'Scissors' or player_choice == 'scissors':
print('The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
if comp_choice == 'Paper' or comp_choice == 'paper':
if player_choice == 'Rock' or player_choice == 'rock':
print('The computer wins!')
winner = 'computer'
elif player_choice == 'Scissors' or player_choice == 'scissors':
print('You win!')
winner = 'player'
else:
print('The game is tied. Try again.')
winner = 'tied'
if comp_choice == 'Scissors' or comp_choice == 'scissors':
if player_choice == 'Rock' or player_choice == 'rock':
print('You win!')
winner = 'player'
elif player_choice == 'Paper' or player_choice == 'player':
print('The computer wins!')
winner = 'computer'
else:
print('The game is tied. Try again.')
winner = 'tied'
return winner
main()
You are trying to translate a string, e.g. rock into an integer (number). This is not possible as Python has no way of knowing the integer you wish to translate it into. Instead you could change your process_player() to the following. I simplified your while statement, and removed the translation to int - Then you check for whether player_choice is equal to the string '2' instead of the integer version, and it should work
def process_player():
player_choice = input('Rock, Paper, Scissors? (Enter 2 if you want to quit):')
# if the player enters 2, quit the game
if player_choice == '2':
print('Thank you for playing!')
# check if user is inputting correct selections
while player_choice.lower() not in ['rock', 'paper', 'scissors', '2']:
print('Error! Please enter an option that was given.')
player_choice = input('Rock, Paper, Scissors? (Enter 2 if you want to quit):')
return player_choice

I have a bug in my program that I cannot figure out

So, I just recently started coding and decided to make a rock paper scissors game; However, my program has a bug where if the user enters "rock" the correct code block doesn't run. Instead it runs an else statement that's only meant to run when the user enters, "no". I tried using a while loop instead of just if else statements but it didn't make a difference.
import random
q_1 = str(input("Hello, want to play Rock Paper Scissors?:"))
print()
# ^^adds an indent
rpc_list = ["rock", "paper", "scissors"]
comp = random.choice(rpc_list)
# ^^randomly selects r, p, or s
user = str(input("Great, select Rock Paper or Scissors:"))
if q_1 != "yes":
if q_1 == comp:
print("Oh No, a Tie!")
elif q_1 == "rock":
if comp == "paper":
print("I Win!")
else:
print("You Win!")
elif q_1 == "paper":
if comp == "rock":
print("You Win!")
else:
print("I Win!")
else:
if comp == "rock":
print("I Win!")
else:
print("You Win!")
else:
print("Ok :(")
There are a few issues with your code.
First of all your code only plays the game if the user doesn't enter "yes". You need to change if q_1 != "yes": to if q_1 == "yes":.
Secondly, your code asks the user to choose rock, paper or scissors regardless of whether they have said they want to play or not. Fix this by moving user = str(input("Great, select Rock Paper or Scissors:")) to under the if q_1 == "yes": if statement.
Thirdly, your code uses q1 instead of user as it should.
Here is how your code should look:
import random
q_1 = str(input("Hello, want to play Rock Paper Scissors?:"))
print()
# ^^adds an indent
rpc_list = ["rock", "paper", "scissors"]
comp = random.choice(rpc_list)
# ^^randomly selects r, p, or s
if q_1 == "yes":
user = str(input("Great, select Rock Paper or Scissors:"))
if user == comp:
print("Oh No, a Tie!")
elif user == "rock":
if comp == "paper":
print("I Win!")
else:
print("You Win!")
elif user == "paper":
if comp == "rock":
print("You Win!")
else:
print("I Win!")
else:
if comp == "rock":
print("I Win!")
else:
print("You Win!")
print("I played:",comp)
else:
print("Ok :(")

Categories