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)
Related
I am fairly new in python coding. I made a simple rock, paper, scissor game using random module. I wrote it in three functions. when I am executing it, I get no error but it is not executing the functions.
what I wrote:
import random
def main():
global comp
global user
global play
play = ''
#preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
#computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
#user inputs their choice
user = input("Rock, Paper or Scissor? ")
def user_choice():
global play
play = input("Do you want to continue? press y or n for yes and no respectively. ")
if play == y:
main()
elif play == n:
print("Thank you for playing!")
exit()
#conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
user_choice()
if __name__ == '__main__':
main()
play_game()
it is not executing play_game() and user_choice().
It only ask for the input.
The first thing which is wrong is that y and n are not under "" thus python is treating them as a variable. the second thing which was wrong is the position of the play game which should come below the main() to get executed. All user_choice() was not indented properly
Try this code
import random
def main():
global comp
global user
global play
play = ''
# preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
# computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
# user inputs their choice
user = input("Rock, Paper or Scissor? ")
def user_choice():
global play
play = input(
"Do you want to continue? press y or n for yes and no respectively. ")
if play == "y":
main()
elif play == "n":
print("Thank you for playing!")
exit()
# conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
user_choice()
if __name__ == '__main__':
main()
play_game()
Create another function main() and call play_game() and user_choice() within main.
The code calls the main() function and this takes an input, but it does not call any of the other functions.
This is why.
Expect that you mean to call play_game() and user_choice() from within main().
Working code.
import random
def main():
global comp
global user
global play
play = ''
#preparing list for computer to select from
game = ['rock', 'paper', 'scissor']
#computer chooses randomly from the list above
comp = random.choice(game)
print(comp)
#user inputs their choice
user = input("Rock, Paper or Scissor? ")
user_choice()
play_game()
def user_choice():
global play
play = input("Do you want to continue? press y or n for yes and no respectively. ")
if play == 'y':
main()
elif play == 'n':
print("Thank you for playing!")
exit()
#conditions to the game
def play_game():
global user
global comp
while True:
if user == comp:
print("Tie!")
print("Computer: ", comp, "and User:", user)
elif user == 'rock':
if comp == 'paper':
print("Rock covers paper, computer wins")
else:
print("rock smashes through scissor, you win")
elif user == 'paper':
if comp == 'rock':
print("paper covers rock, you win!")
else:
print("scissor cuts through paper, computer wins")
elif user == 'scissor':
if comp == 'rock':
print("Rock smashes through scissor, Computer wins!")
else:
print("Scissor cuts through paper")
if __name__ == '__main__':
main()
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
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 Was Practicing Programs Of Python And Then I Made This Rock, Paper, Scissors Program. And The Program Is Running Perfectly But The Problem Is Like This:
I Chose "Rock, Paper, Scissors" Like This Than It Runs.
But If I Enter Rock, Paper, Scissors In Any Other Way It Prints The Else Statment i.e., Invalid Choice.
I Want To Use Upper/Lower Case But How And Where.
import random
user = input("""Tell Me What Do You Choose:
~Rock
~Paper
~Scissors.
-> I Choose : """)
choose = ["Rock", "Paper", "Scissors"]
computer = random.choice(choose)
print(f"Computer Chose {computer}.")
#Function To Compare Choices
def compare(user, computer):
if user == computer:
return("It's A Tie :(....")
elif user == "Rock":
if computer == "Scissors":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Scissors":
if computer == "Paper":
return("User Wins!")
else:
return("Computer Wins!")
elif user == "Paper":
if computer == "Rock":
return("User Wins!")
else:
return("Computer Wins!")
else:
return("User Choose Out Of The Options!")
exit()
print(compare(user, computer))
just transform your input into lowercase with the lower() method
user = user.lower()
that makes sCiSsOrS into scissors
you also need to adjust your if statements to be all lowercase
elif user == "Rock": --> elif user == "rock":
I am attempting to make a basic rock, paper, scissors game. When I input either rock, paper, or scissors, I sometimes have to enter the same thing multiple times for it to continue to the if statements. See code below:
# Rock, Paper, Scissors
player_total = 0
computer_total = 0
def get_computer_hand():
choice = randint(1, 3)
if choice == 1:
return "scissors"
elif choice == 2:
return "paper"
else:
return "rock"
def ask_user():
global player_total
global computer_total
player = input("Enter your hand (stop to stop): ")
if player == "stop":
print("Computer had ", computer_total, "points, you had ", player_total, " points.")
exit(0)
computer = get_computer_hand()
if player == "rock":
if computer == "paper":
return "win"
elif computer == "scissors":
return "lose"
else:
return "tie"
elif player == "paper":
if computer == "paper":
return "tie"
elif computer == "scissors":
return "lose"
else:
return "win"
elif player == "scissors":
if computer == "scissors":
return "tie"
elif computer == "paper":
return "win"
else:
return "lose"
def count_winner():
global player_total
global computer_total
player_total = 0
computer_total = 0
while True:
outcome = ask_user()
if outcome == "win":
print("You won that one.")
player_total += 1
elif outcome == "lose":
print("Computer won that one.")
computer_total += 1
count_winner()
I expect it to work the first time and to continue as usual, but I can't seem to figure out why it just asks "Enter your hand (stop to stop): " instead sometimes when I enter either rock, paper, or scissors.
This is happening because there is a tie happening between the computer and the user. This could be fixed by adding the end with the code of
else outcome == "tie":
print("You have tied with the Computer!")
computer_total += 1
player_total += 1
This would add a point to both sides and if you don't want that just delete the last two lines of my code