Hello I am beginner programmer in python and I am having trouble with this code. It is a rock paper scissors game I am not finished yet but it is supposed to print "I win" if the user does not pick rock when the program picks scissors. But when the program picks paper and the user picks rock it does not print "I win". I would like some help thanks. EDIT - answer was to add indent on else before "i win" thank you everybody.
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")
Need to add else in Scissors condition.
Once Python as entered the elif statement, it will automatically skip the outer else. So, if you want it to print "I win" inside the elif statement, you need to indent your else statement like so:
import random
ask0 = input("Rock, Paper, or Scissors? ")
list0 = ["Rock", "Paper", "Scissors"]
r0 = (random.choice(list0))
print("I pick " + r0)
if ask0 == r0:
print("Tie")
elif ask0 == ("Rock"):
if r0 == ("Scissors"):
print("You Win")
else:
print("I win")
Related
I made a rock paper scissors game (code below). Is there a simpler way of making the code able to read both uppercase and lowercase inputs? So far, it only reads uppercase (i.e. Rock, Paper, Scissors). If the input is lowercase (i.e. rock, paper, scissors), the game won't tell you if you've won or not.
import random
import time
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n")
computerChoice = (random.choice(options))
print("Rock...")
time.sleep(1)
print("Paper...")
time.sleep(1)
print("Scissors...")
time.sleep(1)
print("Shoot! \n")
time.sleep(1)
print("You chose " + yourMove)
print("The computer chose " + computerChoice + "\n")
if yourMove == "Rock" and computerChoice == "Paper":
print("You lose!")
elif yourMove == "Paper" and computerChoice == "Rock":
print("You win!")
if yourMove == "Scissors" and computerChoice == "Rock":
print("You lose!")
elif yourMove == "Rock" and computerChoice == "Scissors":
print("You win!")
if yourMove == "Paper" and computerChoice == "Scissors":
print("You lose!")
elif yourMove == "Scissors" and computerChoice == "Paper":
print("You win!")
if yourMove == computerChoice:
print("It's a draw!")
I would recommend that if you are going to work with strings in a list, leave everything in lowercase.
When capturing the value desired by the user, you could use:
value = input("[Text] ").lower().strip()
Python is a case sensitive language, so
"Paper" != "paper" or " paper " != " paper"
python has many methods to work with strings.
your program can use many of them and work.
here are some solutions.
1- use lower() to make user input all lowercase and your options should be lower cased too.
options = ["rock", "paper", "scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").lower()
2- use upper() to make user input all uppercase and your options should be upper case.
options = ["ROCK", "PAPER", "SCISSORS"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").upper()
3- or you can keep your current options and just use capitalize() to make the first letter uppercase.
options = ["Rock", "Paper", "Scissors"]
yourMove = input("Rock, paper, or scissors? \n ---------- \n").capitalize()
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 :(")
I am just starting coding, growing like a baby each day
It gets tough but reviewing my notes and trying to understand/ memorize code.
Please explain for me what to do for this code.
from random import randint
"""This program will enable the user and computer to start a rock paper scissors game"""
options = ["ROCK", "PAPER", "SCISSORS"]
message = {"tie": "Yawn it's a tie!", "won": "Yay you won!", "lost": "Aww you lost!"}
def decide_winner(user_choice, computer_choice):
print("You chose %s") % (user_choice)
print("PC chose %s") % (computer_choice)
if user_choice == computer_choice:
print(message["tie"]) # tie
# user - paper , pc = rock
elif user_choice == options[1] and computer_choice == options[0]:
print(message["won"])
# user - scissor , pc = paper
elif user_choice == options[2] and computer_choice == options[1]:
print(message["won"])
# user - rock , pc - scissors
elif user_choice == options[0] and computer_choice == options[1]:
print(message["won"])
else:
print("YOU LOSE!")
def play_RPS():
user_choice = input("Enter Rock, Paper, Scissors: ")
computer_choice = options[randint(0,2)]
decide_winner(user_choice, computer_choice)
play_RPS()
A couple notes - elif user_choice == options[0] and computer_choice == options[1]: should be elif user_choice == options[0] and computer_choice == options[2]: Your comments were right but you indexed '1' instead of '2' for SCISSORS.
Python is case sensitive, so 'ROCK' will not equal 'Rock'. You need to possibly add the line
user_choice = user_choice.upper() in the function def play_RPS(): before passing it to
decide_winner(user_choice, computer_choice)
Python 2 uses input for things like variables and raw_input for things like strings.
Instead of:
user_choice = input("Enter Rock, Paper, Scissors: ")
Use
user_choice = raw_input("Enter Rock, Paper, Scissors: ")
Inputs are case sensitive
Your options are
options = ["ROCK", "PAPER", "SCISSORS"]
But if a person types "Rock" it shows that they lose
Instead change
user_choice = raw_input("Enter Rock, Paper, Scissors: ")
To
user_choice = raw_input("Enter Rock, Paper, Scissors: ").upper()
from random import randint
"""This program will enable the user and computer to start a rock paper scissors game"""
options = ["ROCK", "PAPER", "SCISSORS"]
message = {"tie": "Yawn it's a tie!", "won": "Yay you won!", "lost": "Aww you lost!"}
best_of_three = 3
def decide_winner(user_choice, computer_choice):
print('You chose %s' % user_choice)
print("PC chose %s" % computer_choice)
if user_choice == computer_choice:
print(message["tie"]) # tie
# user - paper , pc = rock
elif user_choice == options[1] and computer_choice == options[0]:
print(message["won"])
# user - scissor , pc = paper
elif user_choice == options[2] and computer_choice == options[1]:
print(message["won"])
# user - rock , pc - scissors
elif user_choice == options[0] and computer_choice == options[1]:
print(message["won"])
else:
print("YOU LOSE!")
def play_RPS():
user_choice = input("Enter Rock, Paper, Scissors: ").upper()
computer_choice = options[randint(0, 2)]
decide_winner(user_choice, computer_choice)
play_RPS()
I'm very new to python and am taking an online class. I'm not very familiar with many functions in python so if you could keep it somewhat basic for me to keep track of, I would really appreciate it. I am trying to make a program that runs Rock, Paper, Scissors with a user but the num_games is not being accepted in the "for i in range". Also, I have run it and found that Scissors can beat Rock (THIS IS ANARCHY!). Can anyone assist me? Thanks in advance!
import random
def comp_turn():
comp_move = random.randint(1,3)
if comp_move == 1:
return "Rock!"
elif comp_move == 2:
return "Paper!"
else:
return "Scissors!"
def main():
num_games = int(input("Enter how many games you would like to play: "))
print "You are going to play " + str(num_games) + " games! Here we go!"
num_wins = 0
for i in range(num_games):
user_move = input("Choose either Rock, Paper or Scissors and enter it: ")
cpu_turn = comp_turn()
print "The computer went with: " + cpu_turn
if user_move == 'Rock' and cpu_turn == 'Scissors':
print "You won! Nice job!"
num_wins +=1
elif user_move == 'Paper' and cpu_turn == 'Rock':
print "You won! Nice job!"
num_wins +=1
elif user_move == 'Scissors' and cpu_turn == 'Paper':
print "You won! Nice job!"
num_wins +=1
elif user_move == cpu_turn:
print "Oh! You tied"
else:
print "Whoops! You lost!"
return num_wins
print main()
This should be what you want:
import random
def comp_turn():
return random.choice(['Rock','Paper','Scissors'])
def main():
num_games = int(input("Enter how many games you would like to play: "))
print("You are going to play " + str(num_games) + " games! Here we go!")
num_wins = 0
for i in range(num_games):
user_move = input("Choose either Rock, Paper or Scissors and enter it: ")
cpu_turn = comp_turn()
print("The computer went with: " + cpu_turn)
if user_move == 'Rock' and cpu_turn == 'Scissors': print("You won! Nice job!"); num_wins +=1
elif user_move == 'Paper' and cpu_turn == 'Rock': print("You won! Nice job!"); num_wins +=1
elif user_move == 'Scissors' and cpu_turn == 'Paper': print("You won! Nice job!"); num_wins +=1
elif user_move == cpu_turn: print("Oh! You tied")
else: print("Whoops! You lost!");
return num_wins
print(main())
Or even Better:
import random
def comp_turn():
return random.choice(['Rock','Paper','Scissors'])
def main():
num_games = int(input("Enter how many games you would like to play: "))
print("You are going to play " + str(num_games) + " games! Here we go!")
num_wins = 0
winning=[('Rock','Scissors'),('Paper','Rock'),('Scissors','Paper')]
for i in range(num_games):
user_move = input("Choose either Rock, Paper or Scissors and enter it: ")
cpu_turn = comp_turn()
print("The computer went with: " + cpu_turn)
if (user_move,cpu_turn) in winning:
print('You won!')
num_wins+=1
elif user_move == cpu_turn:
print('Same')
else:
print('You lost!')
return num_wins
print(main())
And another option that's also good:
import random
def comp_turn():
return random.choice(['Rock','Paper','Scissors'])
def main():
num_games = int(input("Enter how many games you would like to play: "))
print("You are going to play " + str(num_games) + " games! Here we go!")
num_wins = 0
d={}.fromkeys([('Rock','Scissors'),('Paper','Rock'),('Scissors','Paper')],'You Won')
for i in range(num_games):
user_move = input("Choose either Rock, Paper or Scissors and enter it: ")
cpu_turn = comp_turn()
print("The computer went with: " + cpu_turn)
if not user_move == cpu_turn:
print(d.get((user_move,cpu_turn),'You lost!'))
else:
print('Same')
return num_wins
print(main())
Here's what you want:
import random
outcome = random.choice(['Rock', 'Paper', 'Scissors'])
print(outcome)
Have you tried removing the '!' in the comp_turn function? It seems like cpu_turn variable will contain either 'Rock!', 'Scissors!' or 'Paper!' while your if-else condition is looking for either 'Rock', 'Scissors' or 'Paper' without the '!'. So, no matter what the player or cpu chooses, it will go into 'else' in the 'for' loop and the player loses.
This is the edited code:
import random
def comp_turn():
comp_move = random.randint(1,3)
if comp_move == 1: return "Rock"
elif comp_move == 2: return "Paper"
else: return "Scissors"
def main():
num_games = int(input("Enter how many games you would like to play: "))
print("You are going to play " + str(num_games) + " games! Here we go!")
num_wins = 0
for i in range(num_games):
user_move = input("Choose either Rock, Paper or Scissors and enter it: ")
cpu_turn = comp_turn()
print("The computer went with: " + cpu_turn)
if user_move == 'Rock' and cpu_turn == 'Scissors':
print("You won! Nice job!")
num_wins +=1
elif user_move == 'Paper' and cpu_turn == 'Rock':
print("You won! Nice job!")
num_wins +=1
elif user_move == 'Scissors' and cpu_turn == 'Paper':
print("You won! Nice job!")
num_wins +=1
elif user_move == cpu_turn:
print("Oh! You tied")
else: print("Whoops! You lost!")
return num_wins
print(main())
Do note that the player's input is case sensitive too. Hope this helps!
I'm very new to Python and decided to set myself a challenge of programming a Rock, Paper, Scissors game without copying someone else's code. However, I need help from a Pythonista grown-up!
I've seen many other variations on Rock, Paper, Scissors, on here but nothing to explain why my version isn't working. My program basically follows this format: set empty variables at start, define 4 functions that prints intro text, receives player input, randomly picks the computer's choice, then assesses whether its a win or a loss for the player.
This is all then stuck in a while loop that breaks once the player selects that they don't want to play anymore. (This bit is working fine)
However, whenever I run the code, it just always gives a draw and doesn't seem to store any data for the computer's choice function call. Does anybody know what I'm doing wrong?
Many thanks!
import random
playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0
def showIntroText():
print('Time to play Rock, Paper, Scissors.')
print('Type in your choice below:')
def playerChoose():
playerInput = input()
return
def computerChoose():
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerPick = 'Paper'
elif randomNumber == 2:
computerPick = 'Scissors'
else:
computerPick = 'Rock'
return
def assessResult():
if playerAnswer == computerAnswer:
print('Draw!')
elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
print('Paper beats Rock. You lose!')
elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
print('Scissors cuts Paper. You lose!')
elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
print('Rock blunts Scissors. You lose!')
else:
print('You win!')
winsTotal += 1
return
while True:
timesPlayed += 1
showIntroText()
playerAnswer = playerChoose()
computerAnswer = computerChoose()
assessResult()
print('Do you want to play again? (y/n)')
playAgain = input()
if playAgain == 'n':
break
print('Thank you for playing! You played ' + str(timesPlayed) + ' games.')
You have missed returning values in most of the case.
** Add 'return playerInput ' in playerChoose() instead of only return.
** Add ' return computerPick ' in computerChoose() instead of return.
** Initialize winsTotal variable before using it as 'winsTotal = 0' in assessResult().
** Variables you have intialized at the start of program are out of scope for functions.
Please check this StackOverFlow link for understanding scope of variables in python.
** Add 'return winsTotal' in assessResult() instead of return.
import random
def showIntroText():
print('Time to play Rock, Paper, Scissors.')
print('Type in your choice below:')
def playerChoose():
playerInput = input()
return playerInput
def computerChoose():
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerPick = 'Paper'
elif randomNumber == 2:
computerPick = 'Scissors'
else:
computerPick = 'Rock'
return computerPick
def assessResult(winsTotal):
if playerAnswer == computerAnswer:
print('Draw!')
elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
print('Paper beats Rock. You lose!')
elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
print('Scissors cuts Paper. You lose!')
elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
print('Rock blunts Scissors. You lose!')
else:
print('You win!')
winsTotal += 1
return winsTotal
total_win = 0
while True:
timesPlayed += 1
showIntroText()
playerAnswer = playerChoose()
computerAnswer = computerChoose()
total_win = assessResult(total_win)
print('Do you want to play again? (y/n)')
playAgain = input()
if playAgain == 'n':
break
print('Thank you for playing! You played ' + str(timesPlayed) + ' games.' + 'Out of which you won '+ str(total_win))
Output:
C:\Users\dinesh_pundkar\Desktop>python c.py
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
You win!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Draw!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"y"
Time to play Rock, Paper, Scissors.
Type in your choice below:
"Rock"
Paper beats Rock. You lose!
Do you want to play again? (y/n)
"n"
Thank you for playing! You played 4 games.Out of which you won 1
add input and return in your functions
def computerChoose And def assessResultreturn None
for Example by this code you can play this game :
import random
playerAnswer = ''
computerAnswer = ''
winsTotal = 0
timesPlayed = 0
def playerChoose():
playerInput = input("insert:")
return playerInput
def computerChoose():
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerPick = 'Paper'
elif randomNumber == 2:
computerPick = 'Scissors'
else:
computerPick = 'Rock'
return computerPick
def assessResult(playerAnswer, computerAnswer):
if playerAnswer == computerAnswer:
print('Draw!')
elif playerAnswer == 'Rock' and computerAnswer == 'Paper':
print('Paper beats Rock. You lose!')
elif playerAnswer == 'Paper' and computerAnswer == 'Scissors':
print('Scissors cuts Paper. You lose!')
elif playerAnswer == 'Scissors' and computerAnswer == 'Rock':
print('Rock blunts Scissors. You lose!')
else:
print('You win!')
return
while True:
timesPlayed += 1
playerAnswer = playerChoose()
computerAnswer = computerChoose()
assessResult(playerAnswer,computerAnswer)
print('Do you want to play again? (y/n)')
playAgain = input()
if playAgain == 'n':
break
print('Thank you for playing! You played ' + str(timesPlayed) + ' games.')
It is always a draw because you aren't returning the answers from your function, both playerAnswer and computerAnswer return None
As some of people said playerChoose() and computerChoose() return with None
Modifidy these statement playerChoose() -> return playerInput
and computerChoose() -> return computerPick
AS well as you have to use global variable. Insert this row
global winsTotal
in the assessResult().