Rock Paper Scissors function not showing up - python

The function is not working and therefore my score is not adding. I would like to run this just like a normal rock paper scissors game. It would be highly appreciated if you are able to solve this problem.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, 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():
computer_choice = random.randint(1,3)
if computer_choice == 1:
computer_choice == "r"
elif computer_choice == 2:
computer_choice == "p"
else:
computer_choice == "s"
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
if user_choice == "r":
if computer_choice == "r":
print("You choose rock. Computer choose rock. Tied")
elif computer_choice == "p":
print("You choose rock. Computer choose paper. You Lose")
computer_score += 1
elif computer_choice == "s":
print("You choose rock. Computer choose scissors. You Win")
user_score += 1
elif user_choice == "p":
if computer_choice == "p":
print("You choose paper. Computer choose paper. Tied")
elif computer_choice == "r":
print("You choose paper. Computer choose rock. You Win")
user_score += 1
elif computer_choice == "s":
print("You choose paper. Computer choose scissors. You lose")
computer_score += 1
elif user_choice == "s":
if computer_choice == "s":
print("You choose scissors. Computer choose scissors. Tied")
elif computer_choice == "r":
print("You choose scissors. Computer choose rock. You lose")
computer_score += 1
elif computer_choice == "p":
print("You choose scissors. Computer choose paper. You win")
user_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
if user_choice in ["Y", "y", "yes", "Yes", "YES"]:
pass
elif user_choice in ["N", "NO", "no", "No", "n"]:
break
else:
break

I liked this problem. Try posting it in github as well.
Here are some improvements that I made it.
import random
user_score = 0
computer_score = 0
def Choose_Option():
user_choice = input("Choose Rock, Paper, Scissors: ")
user_choice=user_choice.lower()
if user_choice in ["rock", "r"]:
user_choice = "r"
elif user_choice in ["paper", "p"]:
user_choice = "p"
elif user_choice in ["scissors", "s"]:
user_choice = "s"
else:
print("I don't understand. Try Again")
Choose_Option()
return user_choice
def Computer_Option():
computer_choice = random.randint(1,3)
dict_option = {
1:'r'
,2:'p'
,3:'s'
}
computer_choice=dict_option.get(computer_choice)
return computer_choice
while True:
print("")
user_choice = Choose_Option()
computer_choice = Computer_Option()
print("")
dict_rule={
'p':'r',
's':'p',
'r':'s'
}
dict_name={
'p':'Paper',
's':'Scissor',
'r':'Rock'
}
if user_choice==computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. Tied")
elif dict_rule[user_choice] == computer_choice:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Win")
user_score += 1
else:
print(f"You choose {dict_name[user_choice]}. Computer choose {dict_name[computer_choice]}. You Lose")
computer_score += 1
print("")
print("Player wins: " + str(user_score))
print("Computer wins: " + str(computer_score))
print("")
user_choice = input("Do you want to play again? (y/n)")
user_choice=user_choice.lower()
if user_choice in ["y", "yes"]:
pass
elif user_choice in ["no","n"]:
break
else:
break

Related

First little program, cant seem to fix the capitalization error

This is my first little program I have created. Can anyone help me with making it so it works with the first letter being capitalized?
# Rock Paper Scissors
import time
import random
user_wins = 0
computer_wins = 0
def RockPaperScissors():
user_choice = input("Enter your choice in a game of Rock, Paper, Scissors: ")
if user_choice == "rock" or user_choice == "paper" or user_choice == "scissors":
print("Good choice, lets see who wins.")
time.sleep(1.5)
else:
print('Please enter either "rock", "paper", or "scissors"')
computer_choice = ["rock", "paper", "scissors"]
rand_choice = random.choice(computer_choice)
print("The computer chose", rand_choice)
if user_choice == "rock" and rand_choice == "scissors":
print("You won!")
elif user_choice == "scissors" and rand_choice == "rock":
print("The computer won, try again.")
elif user_choice == "paper" and rand_choice == "rock":
print("You won!")
elif user_choice == "rock" and rand_choice == "paper":
print("The computer won, try again")
elif user_choice == "scissors" and rand_choice == "paper":
print("You won!")
elif user_choice == "paper" and rand_choice == "scissors":
print("The computer won, try again")
RockPaperScissors()
I also have a slight issue with if you capitalize the first letter, the program does not run as intended. Appreciate the help!

Rock, paper, scissors game in Python - some clarification needed

I am writing a simple game of rock, paper and scissors, where the user competes with a computer. There are 5 tries.
The problems I ran into are :
If I enter something apart from three options, it should return "Invalid Entry". Instead, the program stops
The program never prints "You won the game" or "You lost the game", it finishes after 5 attempts
Other feedback would also be appreciated
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
chosen = input("Make your choice: ")
if chosen == "scissors" or chosen == "Scissors":
element = "scissors"
elif chosen == "paper" or chosen == "Paper":
element = "paper"
elif chosen == "rock" or chosen == "Rock":
element = "rock"
else:
return "Invalid Entry"
computer_choices = ["scissors", "paper", "rock"]
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
return "Congrats, you won the game!"
else:
return "Sorry, you lost"
game()
return statement does not print anything but they return the value from a function and as soon as a return statement executes the function ends up executing that's why whenever the user inputs something invalid the program stop.
Also not use the if statement from if chosen = paper or chosen = Paper instead use .lower() to lower the string.
I have made some changes to your code.
Try this code
import random
def game():
win_count = 0
loose_count = 0
tries = 0
while tries < 5:
element = input("Make your choice: ").lower()
computer_choices = ["scissors", "paper", "rock"]
if element not in computer_choices:
print("Invalid choice")
continue
computer_choice = random.choice(computer_choices)
if element == "scissors" and computer_choice == "paper":
print("Computer chose paper, you chose scissors, you win !")
win_count += 1
tries += 1
elif element == "paper" and computer_choice == "scissors":
print("Computer chose scissors, you chose paper, you loose !")
loose_count += 1
tries += 1
elif element == "paper" and computer_choice == "rock":
print("Computer chose rock, you chose paper, you win !")
win_count += 1
tries += 1
elif element == "rock" and computer_choice == "paper":
print("Computer chose paper, you chose rock, you loose !")
loose_count += 1
tries += 1
else:
print("Whoops, that's a draw, try again")
tries+=1
print("Your Wins: "+ str(win_count))
print("Computer Wins: "+str(loose_count))
if win_count > loose_count:
print("Congrats, you won the game!")
else:
print("Sorry, you lost")
game()

How do I make my basic Rock/Paper/Scissors game with fewer lines of code?

I built this simple rock/paper/scissor game by watching some tutorials. Everything is working fine. The issue is that I want to implement something where if the user and the computer choose the same word, then it should say something like "draw."
I could go ahead and add a bunch of "if" and "else" statements, but I don't want that. Can you guys think of any other way to implement that with fewer lines of code?
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if computer_pick == "rock" and user_pick == "scissors":
print("You lost!")
computer_wins += 1
elif computer_pick == "paper" and user_pick == "rock":
print("You lost!")
computer_wins += 1
elif computer_pick == "scissors" and user_pick == "paper":
print("You lost!")
computer_wins += 1
else:
print('You win!')
user_wins += 1
You could use a dictionary to map which choices beat each other. This will enable to you make the conditional part of the code, which determines who wins, more concise. It is also more easily expanded to rock-paper-scissors variants with more options, without the need for many more conditional statements.
#Simple rock, paper and scissor game
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
beaten_by = {
"rock": "scissors",
"scissors": "paper",
"paper": "rock"
}
while True:
user_pick = input("Please choose Rock/Paper/Scissors Or Press Q to quit: ")
if user_pick.lower() == "q":
print("You quit.")
break
elif user_pick not in options:
print("Please enter a valid input.")
continue
random_number = random.randint(0, 2)
computer_pick = options[random_number]
print("The computer picked", computer_pick)
if user_pick == computer_pick:
print("Draw!")
elif user_pick == beaten_by[computer_pick]:
print("You lost!")
computer_wins += 1
else:
print("You win!")
user_wins += 1

Variable not registering change in value

I wanted to make a simple Rock, Paper, Scissor game in Python. It goes well with the game, but the final scores are always being showed as a 0.
I wanted to show a smaller section of the code but, I don't know where the problem lies, so I am sorry for the length of the code.
I am still a novice learner, so please pardon me if the question is too silly or the code is not well formatted.
#Rock-Paper-Scissor Game
import random
print("Please enter your name:")
userName = input()
print("Welcome " + userName)
print("The following are the rules of the game:")
print("Press 'R' for Rock")
print("Press 'P' for Paper")
print("Press 'S' for Scissor")
print("This will be a 10 point match")
userTally = 0
compTally = 0
def gameProcess(userTally, compTally): #The process of the game. It increments or decrements the value depending on the result
print("Your turn:")
userInput = input()
computerChoice = random.choice(["R","P","S"])
if userInput == "R": #User Inputs R for Rock
if computerChoice == "R":
print("The computer chose Rock")
print("It's a Tie")
elif computerChoice == "P":
print("The computer chose Paper")
print("Computer Won")
compTally = compTally + 1
elif computerChoice == "S":
print("The computer chose Scissor")
print("You Won")
userTally = userTally + 1
elif userInput == "P": #User Inputs P for Paper
if computerChoice == "R":
print("The computer chose Rock")
print("You Won")
userTally = userTally + 1
elif computerChoice == "P":
print("The computer chose Paper")
print("It's a Tie")
elif computerChoice == "S":
print("The computer chose Scissor")
print("Computer Won")
compTally = compTally + 1
elif userInput == "S": #User Inputs S for Scissor
if computerChoice == "R":
print("The computer chose Rock")
print("Computer Won")
compTally = compTally + 1
elif computerChoice == "P":
print("The computer chose Paper")
print("You Won")
userTally = userTally + 1
elif computerChoice == "S":
print("The computer chose Scissor")
print("It's a Tie")
return(userTally,compTally)
def tryCount(): #The number of tries....
tryNum = 1
while tryNum < 11:
gameProcess(0, 0)
tryNum = tryNum + 1
tryCount()
print("You scored " + str(userTally))
print("The computer scored " + str(compTally))
if userTally > compTally:
print("CONGRATULATIONS, YOU WON.")
elif userTally < compTally:
print("Sorry, better luck next time.")
close = input()
if close == "Random Input.":
exit()
else:
exit()
You pass 0, 0 to gameProcess, which you treat as the scores within the function, and then return them modified, but you do not actually use the return value in the only place you call gameProcess (in tryCount), so the global userTally, compTally variables remain unchanged.
This is how you should change tryCount:
def tryCount(): #The number of tries....
global userTally, compTally
tryNum = 1
while tryNum < 11:
userTally,compTally=gameProcess(userTally,compTally)
tryNum = tryNum + 1

Rock Paper Scissors Python Program

I'm trying to write a Rock, Paper Scissors program with Python but I keep getting this error and I'm not sure how to fix it.
File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 140, in <module>
File "C:\Python33\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 34, in main
builtins.TypeError: 'tuple' object is not callable
This is my program:
import random
def main():
win = 0
lose = 0
tie = 0
ROCK_CHOICE = '1'
PAPER_CHOICE = '2'
SCISSORS_CHOICE = '3'
QUIT_CHOICE = '4'
play_again = 'y'
while play_again == 'y':
print('Welcome to the game of paper, rock, scissors!')
print('Please input the correct number according')
print('to the choices given.')
computer_choice = get_computer_choice()
player_choice = get_player_choice()
determine_winner = (computer_choice, player_choice)
print('Computer choose', computer_choice, '.')
print('You choose', player_choice, '.')
determine_winner(computer_choice, player_choice)
if result == -1:
lose += 1
elif result == 0:
tie += 1
else:
win += 1
play_again = input('Play again? Enter y for yes')
def get_computer_choice():
choice = random.randint(1,4)
if choice == 1:
choice = 'ROCK'
elif choice == 2:
choice = 'PAPER'
elif choice == 3:
choice = 'SCISSORS'
else:
choice = 4
return choice
def get_player_choice():
choice = int(input('Select rock(1), paper(2), or scissors(3): '))
while choice != 1 and choice != 2 and choice != 3:
print('The valid numbers are rock choice 1), paper choice 2),')
print('or scissors choice 3).')
choice = int(input('Please a valid number: '))
if choice == 1:
choice = 'ROCK'
elif choice == 2:
choice = 'PAPER'
elif choice == 3:
choice = 'SCISSORS'
else:
choice = 4
return choice
def determine_winner(computer_choice, player_choice):
if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
print('Its a tie.')
return 0
elif player_choice == PAPER_CHOICE and computer_choice == PAPER_CHOICE:
print('Its a tie.')
return 0
elif player_choice == SCISSORS_CHOICE and computer_choice == SCISSORS_CHOICE:
print('Its a tie.')
return 0
elif player_choice == ROCK_CHOICE and computer_choice == PAPER_CHOICE:
print('You lose, Rock covers Paper.')
return -1
elif player_choice == ROCK_CHOICE and computer_choice == SCISSORS_CHOICE:
print('You WIN!!! Rock smashes Scissors.')
return 1
elif player_choice == PAPER_CHOICE and computer_choice == SCISSORS_CHOICE:
print('You lose, Scissors cuts Paper.')
return -1
elif player_choice == SCISSORS_CHOICE and computer_choice == ROCK_CHOICE:
print('You lose, Rock smashes Paper.')
return -1
elif player_choice == SCISSORS_CHOICE and computer_choice == PAPER_CHOICE:
print('You WIN!!! Scissors cuts Paper.')
return 1
elif player_choice == PAPER_CHOICE and computer_choice == ROCK_CHOICE:
print('You WIN!!! Paper covers Rock.')
return 1
else:
player_choice == QUIT_CHOICE
print('Exiting the program...')
return
def display_results():
print()
print(' MENU')
print('1) Rock')
print('2) Paper')
print('3) Scissors')
print('4) Quit')
player_choice = input('Enter your choice:')
while player_choice != '1' and player_choice != '2' and \
player_choice != '3' and player_choice != '4':
print()
print('Error: invalid selection.')
player_choice = input('Please re-enter your choice:')
print('Your total wins are', win, '.')
print('Your total losses are', lose, '.')
print('Your total ties are', tie, '.')
main()
input('\nPress ENTER to continue...')
error 1:
fix line27 : result = determine_winner(computer_choice, player_choice
error 2:
if player_choice == ROCK_CHOICE and computer_choice == ROCK_CHOICE:
NameError: global name 'ROCK_CHOICE' is not defined
move:
ROCK_CHOICE = '1'
PAPER_CHOICE = '2'
SCISSORS_CHOICE = '3'
QUIT_CHOICE = '4'
outside main (to line 3)
error 3:
File "main.py", line 41, in main
play_again = input('Play again? Enter y for yes')
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
input should be raw_input line 41:
play_again = raw_input('Play again? Enter y for yes')
NB. and the input('\nPress ENTER to continue...') also needs to be a raw_input
NNBB. and if you plan to use display_results; there is errors in there too.
from random import randint
class RPS:
def __init__(self, pCh):
self.playChoice = pCh
self.compChoice = ""
self.choice = randint(0, 3)
self.winner = ""
self.uCompChoice = ""
self.uPlayChoice = ""
if self.choice == 0:
self.compChoice = "R"
elif self.choice == 1:
self.compChoice = "P"
else:
self.compChoice = "S"
if self.compChoice == "R":
self.uCompChoice = "Rock"
if self.compChoice == "P":
self.uCompChoice = "Paper"
if self.compChoice == "S":
self.uCompChoice = "Scissors"
if self.playChoice == "P" or self.playChoice == "p" or self.playChoice == "Paper" or self.playChoice == "paper" or self.playChoice == "PAPER" or self.playChoice == "2":
self.uPlayChoice = "Paper"
if self.playChoice == "S" or self.playChoice == "s" or self.playChoice == "Scissors" or self.playChoice == "scissors" or self.playChoice == "SCISSORS" or self.playChoice == "3":
self.uPlayChoice = "Scissors"
if self.playChoice == "R" or self.playChoice == "r" or self.playChoice == "Rock" or self.playChoice == "rock" or self.playChoice == "ROCK" or self.playChoice == "1":
self.uPlayChoice = "Rock"
def determineWinner(self):
if self.uCompChoice == self.uPlayChoice:
return "Draw Game!"
elif self.uCompChoice == "Rock":
if self.uPlayChoice == "Paper":
return "You won because Paper covers Rock!!"
else:
return "Computer wins because Rock breaks Scissors."
elif self.uCompChoice == "Paper":
if self.uPlayChoice == "Scissors":
return "You won because Rock cuts Paper!!"
else:
return "Computer wins because Paper covers Rock."
else:
if self.uPlayChoice == "Rock":
return "You won because Rock breaks Scissors!!"
else:
return "Computer wins because Scissors cuts Paper."
def __str__(self):
return "Your weapon: " + self.uPlayChoice + "\n" + "Computer's weapon " + self.uCompChoice + "\n" + \
self.determineWinner()
def main():
print("Welcome to RockPaperScissors.")
print("Pick your weapon: ")
print("1. Rock")
print("2. Paper")
print("3. Scissors")
print("Note: If you don't pick a valid weapon the computer will pick a random weapon for you")
# input from user (weapon)
weapon = input("")
# Creating a new object
attack = RPS(weapon)
# printing the result
print(attack.__str__())
# Asking the user if he wants to play again
playagain = input("Would you like to play again: \n")
if playagain == "Yes" or playagain == "yes" or playagain == "YES" or playagain == "y" or playagain == "Y":
main()
else:
print("Thank you for playing.")
main()
don't you think, this code will be shorter this way:
print("R for rock")
print("P for paper")
print("S for scissors")
choices = ["rock", "paper", "scissor"]
while True:
choice = random.choice(choices)
player_choice = input("choose> ")
if player_choice.lower() == "r":
if choice == "rock":
print("i picked rock too!")
elif choice == 'paper':
print("YOU LOST! i picked paper")
else:
print("fine, you win")
elif player_choice.lower() == "p":
if choice == "rock":
print("You win.... Whatever!")
elif choice == 'paper':
print("haha same decisions")
else:
print("I won! You should be ashamed of losing against a bot")
elif player_choice.lower() == "s":
if choice == "rock":
print("I won! Humanity will soon be controlled by bots")
elif choice == 'paper':
print("Alright, you win")
else:
print("Draw")
else:
print("YOU WANNA PLAY GAMES WITH ME!??!?!?")

Categories

Resources