Reputation: 57
hi i am practicing python and doing rps game. This is the code:
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = raw_input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
if player_choice == 1:
player_choice = Choices.rock
if player_choice == 2:
player_choice = Choices.paper
if player_choice == 3:
player_choice = Choices.scissors
# Getting the cpu choice.
cpu_choice = random.randint(1, 3)
if cpu_choice == 1:
cpu_choice = Choices.rock
if cpu_choice == 2:
cpu_choice = Choices.paper
if cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
if player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
if player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
if player_choice == Choices.rock and cpu_choice == Choices.scissors:
print"\n Congratulations!\n{} you won!".format(name)
else:
print"\n Too Bad You Lost!".format(name)
print "\nCPU Choice: {}\n Your Choice: {}".format(cpu_choice, player_choice)
play_again = raw_input("Want to Play Again? y/n")
if play_again == 'y':
continue
else:
break
I defined only when the player is winning or its a tie and for the else i did else statement. But and a big But it will always will output always the lose output for some reason and when its print the choices for the CPU it prints a string the describes the choice(Paper Scissors ...) But for the players choice it prints the number So Id be happy to get Your opinion what am i doing wrong and ALSO Id be happy to get some tips what are your thoughts and tips to get my code more efficient and professional
Upvotes: 0
Views: 100
Reputation: 21609
In your existing code. The else only applies to the final if statement. So if the player choice is not Rock
and the cpu choice is not Scissors
then the else is printed.
To resolve this use elif to make a chain of if statements. Then the else will apply only if none of the if conditions are met e.g.
if player_choice == cpu_choice:
print"\n Its a Tie!/n{} you!".format(name)
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print"\n Congratulations!\n{} you won!".format(name)
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print"\n Congratulations!\n{} you won!".format(name)
else:
print"\n Too Bad You Lost!".format(name)
You also need to convert the return value from raw_input
to an int. Otherwise its a string and will never be equal to any of the integer values representing the choices. So use int(raw_input())
instead.
I see you tried to do this by using int(player_choice)
. The int constructor does not work 'in place'. i.e. it doesnt convert the variable itself to an int. It returns an integer value leaving player_choice
as a string. You can see when we wrap input
in the call to int
we are capturing the return value and so player_choice
is then an int (assuming your input was convertible to int).
Here's a that will work on python 2 and python 3.
import random
from sys import version_info
if version_info.major == 2:
try:
input = raw_input
except NameError:
pass
class Choices:
rock = "Rock"
paper = 'Paper'
scissors = 'Scissors'
def rps_game():
# Getting the name of the player.
name = input("Welcome to Rock Paper Scissors Game!\n Enter your name:")
# The Players Choice.
while True:
player_choice = int(input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name)))
if player_choice == 1:
player_choice = Choices.rock
elif player_choice == 2:
player_choice = Choices.paper
elif player_choice == 3:
player_choice = Choices.scissors
# Getting the cpu choice.
cpu_choice = random.randint(1, 3)
if cpu_choice == 1:
cpu_choice = Choices.rock
elif cpu_choice == 2:
cpu_choice = Choices.paper
elif cpu_choice == 3:
cpu_choice = Choices.scissors
if player_choice == cpu_choice:
print("\n Its a Tie!/n{} you!".format(name))
elif player_choice == Choices.paper and cpu_choice == Choices.rock:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.scissors and cpu_choice == Choices.paper:
print("\n Congratulations!\n{} you won!".format(name))
elif player_choice == Choices.rock and cpu_choice == Choices.scissors:
print("\n Congratulations!\n{} you won!".format(name))
else:
print("\n Too Bad You Lost!".format(name))
print ("\nCPU Choice: {}\n Your Choice: {}".format(cpu_choice, player_choice))
play_again = input("Want to Play Again? y/n")
if play_again != 'y':
break
rps_game()
Also here's an attempt to implement it in less code.
from __future__ import print_function
import random
from sys import version_info
if version_info.major == 2:
input = raw_input
choices = ('Rock', 'Paper', 'Scissors')
winlogic = {
'Rock': 'Scissors',
'Paper': 'Rock',
'Scissors': 'Paper'
}
def calcwinner(choice1, choice2):
if choice1 == choice2: return 0
return 1 if winlogic[choice1] == choice2 else -1
name = input("Welcome to Rock Paper Scissors Game!\nEnter your name: ")
while True:
index = int(input("1-Rock\n2-Paper\n3-Scissors\n{} choose a number: ".format(name)))
try:
player_choice = choices[index - 1]
except IndexError:
print('please make a valid choice')
continue
cpu_choice = random.choice(choices)
winner = calcwinner(player_choice, cpu_choice)
print('You chose {} and computer chose {}. '.format(player_choice, cpu_choice), end='')
if winner == 0:
print('Tie')
elif winner < 0:
print('Cpu won')
else:
print('You won!')
play_again = input("Want to Play Again? y/n: ")
if play_again not in ('y', 'Y', 'yes'):
break
Upvotes: 1
Reputation: 11
if playerChoice == 1 and compChoice == 3:
print("You Win - Rock crushes Scissors.")
if playerChoice == 2 and compChoice == 1:
print("You Win - Paper covers Rock.")
if playerChoice == 3 and compChoice == 2:
print("You Win - Scissors cut Paper.")
if compChoice == 1 and playerChoice == 3:
print("You Lose - Rock crushes Scissors.")
if compChoice == 2 and playerChoice == 1:
print("You Lose - Paper covers Rock.")
if compChoice == 3 and playerChoice == 2:
print("You Lose - Scissors cut Paper.")
Upvotes: 1
Reputation: 12157
player_choice = raw_input("\n1-Rock\n2-Paper\n3-Scissors\n{} choose a number:".format(name))
int(player_choice)
This code doesn't work. player_choice
is set as a string from raw_input
but int(player_choice)
doesn't do anything. It creates an integer and then sends it into the void. Instead, you need to reassign it to player_choice
like so:
player_choice = int(player_choice)
Upvotes: 1