I'm trying to write a rock-paper-scissors game in python.this is the code:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
play = False
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while play == False:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ",computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
break
again = input("do you want another round?:")
if again == "yes":
play = False
else:
play = True
the program works well but I want it to ask the player whether or not he/she wants to do another round.If the answer is yes the program must restart the loop.
The problem is I don't know how to do that.I know it's probably related to True and False and I attempted to do something as you can see in the code but it didn't work.
please Help me.
A simple fix might be just putting your while loop as True and continuing to loop until you break the execution:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while True:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ", computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
again = input("do you want another round?:")
if again != "yes":
break
Related
I am looking for ways to shorten and simplify my python code. Here is one example of one small rock/paper/scissors game I wrote earlier. The code kinda looks too long to me and I want to try to learn how to shorten my code.
import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")
while True:
user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
break
elif user_input not in options:
print("type in a valid word next time")
continue
game_limit += 1
if game_limit == 5 and comp_wins > user_wins:
print("The game is over, YOU LOST!")
elif game_limit == 5 and comp_wins < user_wins:
print("The game is over, YOU WON!")
random_number = random.randint(0,2)
comp_input = options[random_number]
print("Computer picked", comp_input)
if user_input == "rock" and comp_input == "scissors":
print("You win")
user_wins += 1
elif user_input == "rock" and comp_input == "rock":
print("its a draw")
elif user_input == "rock" and comp_input == "paper":
print("You lose!")
comp_wins += 1
if user_input == "scissors" and comp_input == "paper":
print("You win")
user_wins += 1
elif user_input == "scissors" and comp_input == "scissors":
print("its a draw")
elif user_input == "scissors" and comp_input == "rock":
print("You lose!")
comp_wins += 1
if user_input == "paper" and comp_input == "rock":
print("You win")
user_wins += 1
elif user_input == "paper" and comp_input == "paper":
print("its a draw")
elif user_input == "paper" and comp_input == "scissors":
print("You lose!")
comp_wins += 1
Yes. You could simplify your logic by having compound and/or tests for the player and user. Following is a revised version of your code with some simplified logic.
import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")
while True:
if game_limit >= 5: # Tweak
if comp_wins > user_wins:
print("The game is over, YOU LOST!")
break
if comp_wins < user_wins:
print("The game is over, YOU WON!")
break
else:
print("We go to sudden death!")
user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
break
elif user_input not in options:
print("type in a valid word next time")
continue
game_limit += 1
random_number = random.randint(0,2)
comp_input = options[random_number]
print("Computer picked", comp_input)
if user_input == "rock" and comp_input == "scissors" or user_input == "paper" and comp_input == "rock" or user_input == "scissors" and comp_input == "paper":
print("You win")
user_wins += 1
elif user_input == "rock" and comp_input == "paper" or user_input == "paper" and comp_input == "scissors" or user_input == "scissors" and comp_input == "rock":
print("You lose!")
comp_wins += 1
else:
print("It's a draw")
This way all of the scenarios where the user can win are in one logical test and all of the scenarios where the computer can win are in another logical test. If neither test is true, then the default would have to be a draw.
Also, I tweaked game limit test because if the computer and user have the same score when the game limit is reached, the test result would be false and the game would then continue on and not stop. So there is a bit of additional code to handle a tie after five rounds.
Give that a try.
WHile I was attempting a simple rock, paper scissors, I ran into this proble. When I execute the file and input the user_input, it says to me that the variable computer_choice doesn’t exist, even though it does exist. I would appreciate if someone could help me, thank you.
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit. ").lower()
if user_input == "q":
break
if user_input not in options:
continue
random_number = random.randint (0, 2)
computer_choice = options[random_number]
print ("The computer picked:", computer_choice + ".")
if user_input == "rock" and computer_choice == "scissors" :
print("You Won!")
user_wins += 1
continue
elif user_input == "scissors" and computer_choice == "paper" :
print("You Won!")
user_wins += 1
continue
elif user_input == "paper" and computer_pick == "rock" :
print("You Won!")
user_wins += 1
continue
else:
print("You Lost!")
computer_wins += 1
continue
print("You won", user_wins,"times and the computer won", computer_wins, "times.")
print("Goodbye")
You have to be careful with indentation when using python:
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit. ").lower()
if user_input == "q":
break
if user_input not in options:
continue
# not at this level
# at this level
random_number = random.randint(0, 2)
computer_choice = options[random_number]
print("The computer picked:", computer_choice + ".")
if user_input == "rock" and computer_choice == "scissors":
print("You Won!")
user_wins += 1
continue
elif user_input == "scissors" and computer_choice == "paper":
print("You Won!")
user_wins += 1
continue
elif user_input == "paper" and computer_choice == "rock": #not computer_pick
print("You Won!")
user_wins += 1
continue
Also you wrote computer_pick in line 31, probably you mean computer_choice
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)
could you tell me why my program doesnt increment "bot_points = 0"
"your_points = 0" to stop the while loop when some of these values reach 3
import random
rock_paper_scissors = ["ROCK", "PAPER", "SCISSORS"]
bot_points = 0
your_points = 0
while bot_points <= 3 or your_points <= 3:
user_choice = input('type "rock", "paper" or "scissors": ').upper()
bot_choice = random.choice(rock_paper_scissors)
if bot_choice == "ROCK" and user_choice == "ROCK":
print("Draw")
elif bot_choice == "ROCK" and user_choice == "PAPER":
print("You win")
your_points = your_points+1
elif bot_choice == "PAPER" and user_choice == "PAPER":
print("Draw")
elif bot_choice == "PAPER" and user_choice == "SCISSORS":
print("You win")
your_points= your_points+1
elif bot_choice == "PAPER" and user_choice == "ROCK":
print("You lose")
bot_points = bot_points+1
elif bot_choice == "SCISSORS" and user_choice == "PAPER":
print("You lose")
bot_points = bot_points+1
elif bot_choice == "SCISSORS" and user_choice == "ROCK":
print("You win")
your_points = your_points+1
elif bot_choice == "SCISSORS" and user_choice == "SCISSORS":
print("Draw")
elif bot_choice == "ROCK" and user_choice == "SCISSORS":
print("You lose")
bot_points = bot_points+1
Two issues:
You test for <= 3, so it won't end until someone wins four times
You test bot_points <= 3 or your_points <= 3, so it won't end until both players win at least four times; and would end when either reaches the threshold
If you want to end when either player wins three times, change the test to:
while bot_points < 3 and your_points < 3:
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
So i wrote my first Rock, Paper, Scissors in Python and it nearly works. The problem is that it sometimes reads "scissors" as an invalid input, which seems to me completely randomly and i dont know what the problem is. Hoping for an easy fix for it. Here's the code:
import time
import random
choice_list = ["Rock", "Paper", "Scissors"]
def game():
won = False
lost = False
while not won and not lost:
user = input("Enter your choice from Rock, Paper or Scissors: ")
user_i = user.lower()
com_1 = random.choice(choice_list)
com_choice = com_1.lower()
if com_choice == user_i:
print("Draw, try again!")
elif com_choice == "rock" and user_i == "paper":
won = True
elif com_choice == "rock" and user_i == "scissors":
lost = True
elif com_choice == "paper" and user_i == "rock":
lost = True
elif com_choice == "paper" and user_i == "scissors":
won = True
elif com_choice == "scissors" and user_i == "rock":
won = True
elif com_choice == "scissors" and user_i == "paper":
lost = True
elif user != "scissors" or "paper" or "rock":
print("Invalid choice, try again...")
if won:
print("Congrats you won, the PC chose " + com_1 + "!")
won = False
if lost:
print("You lost, the PC chose " + com_1 + "!")
lost = False
play_again = "yes"
while play_again.lower() == "yes":
game()
play_again = input("Would u like to play again? ")
if play_again.lower() != "yes":
print("Goodbye!")
time.sleep(3)
The fix:
elif user_i not in ("scissors" or "paper" or "rock"):
print("Invalid choice, try again...")
You were using user instead of user_i. So, when user is Scissors, the elif condition will fail("Scissors" != "scissors") and return Invalid choice.
Also,
elif user != "scissors" or "paper" or "rock" is evaluated as
elif (user != 'scissors') or 'paper' or 'rock' which is not exactly what you intend although it doesn't affect your results. Instead, you should write
elif user_i not in ("scissors", "paper", "rock"):
Actually, you don't even need the last elif since you have already checked for all the valid inputs in the previous if-else statements.
In your program, when validating input for invalid words, you are using raw input user. This could have different case from user_i = user.lower().
Try with the below line:
elif ((user_i != "scissors") and (user_i != "paper") and (user_i != "rock")):
print("Invalid choice, try again...")
If still you get issues, pls let us know your input.