Trying to make a rock paper scissor game with a player versing the computer to refresh knowledge on coding however I can't seem to find what's causing the issue in my while loop. I have it set to finish when either the computer or person reaches a score of 3 but stops whenever the total scores equal 8. Full code is listed below
import random
rock = "rock"
paper = "paper"
scissors = "scissors"
humanscore = int(0)
compscore = int(0)
limit = int(3)
comp = ["paper", "rock", "scissors"]
while humanscore <= limit or compscore <= limit:
human = str(input("Choose rock, paper, or scissors, first to 3 wins! "))
answer = random.choice(comp)
if human == answer:
print("Tie, Computer chose, ", answer)
if answer == rock:
if human == paper:
humanscore += 1
print("You Win!")
elif human == scissors:
compscore += 1
print("Computer Won! Try again")
if answer == paper:
if human == rock:
compscore += 1
print("Computer Won! Try again")
elif human == scissors:
humanscore + 1
print("You Win!")
if answer == scissors:
if human == paper:
compscore += 1
print("Computer Won! Try again")
elif human == rock:
humanscore += 1
print("You Win!")
print("\n Computer Chose: ", answer, "computer score: ", compscore, "Human Score:", humanscore)
As already said in comments, the problem is with the or in your break condition. As long as one of the scores is below or equal 3, it keeps running.
For instance, if humanscore = 3 and comp score = 2, both parts of the condition are true meaning it continues.
As soon as humanscore = 4 and compscore = 4, both humanscore <= 3 and compscore <= 3 evaluate to false, meaning it stops => total score is 8.
Hence, the while should be like follows (also it should be < instead of <=, because you want to stop as soon as one has 3 points):
while humanscore < limit and compscore < limit:
Once you've sorted out the loop - which if you only want to keep going while the scores are both below limit is corrected as per schilli's answer - you might like to consider a data structure suggestion:
Make a dict that records what choice beats a given choice:
whatBeats = {rock:paper, paper:scissors, scissors:rock}
# then later after choices are made...
if answer == whatBeats[human]:
# computer win
elif human == whatBeats[answer]:
# human win
else:
# draw
(Many decades ago I wrote a sorting program before I had learned about any indexed structure - like arrays - and your current method puts me in mind of that sort of effort).
Related
I am trying my hand a developing the code for this game.
After deciding how many rounds (3, 5 or 7), I ask for input on which move the user will do. After comparison with the ai's choice, I want the score to be displayed each round.
The way I want to display the score is as a list type :
score = [ai_score, player_score].
However, whenever I try to increment a player's score, when I call
print (score)
the list is not updated with the values. Each round the print returns [0, 0]
My question is : Can you put variables in a list and then increment those variables?
Thank you.
Please see code :
import random
ai_score, player_score=0,0
score = [ai_score, player_score]
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))
for round in range(rounds):
player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
ai_choice = random.choice(game)
if player_choice in game:
if player_choice == ai_choice:
print("it's a draw")
elif player_choice == 'R':
if ai_choice == 'S':
player_score += 1
print('You win the round!')
elif ai_choice == 'P':
ai_score += 1
print("You loose this round")
elif player_choice == 'P':
if ai_choice == 'R':
player_score += 1
print('You win the round!')
elif ai_choice == 'S':
ai_score += 1
print("You loose this round")
elif player_choice == 'S':
if ai_choice == 'P':
player_score += 1
print('You win the round!')
elif ai_choice == 'R':
ai_score += 1
print("You loose this round")
print("This is round %d" % round)
print("The score is %s" % score)
print(score)
else:
print('Please enter one of the 3 choices')
No, updating the variables will not update the values of the list. To keep the values updated inside the list, you can declare the list inside the loop.
import random
ai_score, player_score = 0, 0
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))
for round in range(rounds):
player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
ai_choice = random.choice(game)
if player_choice in game:
if player_choice == ai_choice:
print("it's a draw")
elif player_choice == 'R':
if ai_choice == 'S':
player_score += 1
print('You win the round!')
elif ai_choice == 'P':
ai_score += 1
print("You loose this round")
elif player_choice == 'P':
if ai_choice == 'R':
player_score += 1
print('You win the round!')
elif ai_choice == 'S':
ai_score += 1
print("You loose this round")
elif player_choice == 'S':
if ai_choice == 'P':
player_score += 1
print('You win the round!')
elif ai_choice == 'R':
ai_score += 1
print("You loose this round")
print("This is round %d" % round)
score = [ai_score, player_score]
print("The score is %s" % score)
print(score)
else:
print('Please enter one of the 3 choices')
Output:
Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?3
Please choose your move : R for rock, P for paper, S for scissors :R
You win the round!
This is round 0
The score is [0, 1]
[0, 1]
Please choose your move : R for rock, P for paper, S for scissors :P
You loose this round
This is round 1
The score is [1, 1]
[1, 1]
Please choose your move : R for rock, P for paper, S for scissors :S
You loose this round
This is round 2
The score is [2, 1]
[2, 1]
Explanation:
When we create a list with variables, the list puts the values of those variables; not the memory address. So, updating the variables, later on, will not affect the list.
In the above scenario, we have assigned initial score to zero for ai_score and player_score using ai_score, player_score = 0, 0. Then, we have assigned the value of these variables to score list using score = [ai_score, player_score]. Later, if we update the values of ai_score and player_score, the score list will not be updated. We need to update the score list with new values of ai_score and player_score.
Original code:
score = [ai_score, player_score]
In order to update any individual item in a list, you add square brackets next to it with the item index within them, then set that equal to whatever value you want to change it to.
In your case, to change the ai score to 5:
score[0] = 5
Where 0 is the index (the index starts a 0 so the player score index would be 1)
I am new to coding so I am trying to create a Rock Paper Scissors Game. I have almost completed the game, but when the user and computer input the same number, I want the program to repeat until either one of the player wins. How can I do this? Any help is appreciated. Here is my code.
game = input("Want to play Rock Paper Scissors? (Y/N) ")
if game == "Y":
print("1 = Rock, 2 = Paper, 3 = Scissors")
print('')
user = int(input("You have one chance of beating me. Input a number. "))
print('')
import random
computer = random.randint(1,3)
if user == 1 and computer == 1:
print("Must play again! We both played Rock!")
elif user == 1 and computer == 2:
print("You lose! You played Rock and I played Paper!")
elif user == 1 and computer == 3:
print("You win! You played Rock and I played Scissors!")
elif user == 2 and computer == 1:
print("You win! You played Paper and I played Rock!")
elif user == 2 and computer == 2:
print("Must play again! We both played Paper!")
elif user == 2 and computer == 3:
print("You lose! You played Paper and I played Scissors!")
elif user == 3 and computer == 1:
print("You lose! You played Scissors and I played Rock!")
elif user == 3 and computer == 2:
print("You win! You played Scissors and I played Paper!")
elif user == 3 and computer == 3:
print("Must play again! We both played Scissors!")
else:
print("Not a number.")
else:
print("Fine. Bye.")
You could put the whole if-elif-block in a while-loop that will repeat until you have a winner. In order to determine if there is a winner, use a boolean variable.
winner = False
while not winner:
if ...... ## Your if-elif-block
elif user == 1 and computer == 2:
print("You lose! You played Rock and I played Paper!")
winner = True
## Your remaining if-elif-block
You only put the command winner=True in the command blocks of the conditions that have a winner. So, the loop will continue until you hit one of these conditions.
You could also choose to use a more advanced winner variable (0 for draw, 1 for player, 2 for computer) to use the value in a goodbye-message.
One way would be to use a while loop, breaking when a condition is satisfied.
while True:
if (condition):
print("")
break
...
The while statement repeats the loop until one of the conditions is satisfied. The break statement causes the program to exit the loop and move on to the next executable statement.
(to learn more google anything in quotes :)
using a "while loop" to create a "game loop". I learned this trick from making small games in python. Also, you want to learn to use "classes" as the logic and the code can be improved using "OOP concepts".T code has been tested and works.
import random
#Create a "function" that meets the requirements of a "game loop"
def gameloop():
game = input("Want to play Rock Paper Scissors? (Y/N) ")
if game == "Y":
#Create a "while loop" to host the logic of the game.
#Each If statement will enable one "rule" of the game logic.
#game logic could be redesigned as an "Event".
#You can add a game "Event System" to your future project backlog
winner = False
while not winner:
print("1 = Rock, 2 = Paper, 3 = Scissors")
print('')
user = int(
input("You have one chance of beating me. Input a number. "))
print('')
computer = random.randint(1, 3)
if user == 1 and computer == 2:
print("You lose! You played Rock and I played Paper!")
winner = True
elif user == 1 and computer == 3:
print("You win! You played Rock and I played Scissors!")
winner = True
elif user == 2 and computer == 1:
print("You win! You played Paper and I played Rock!")
winner = True
elif user == 2 and computer == 3:
print("You lose! You played Paper and I played Scissors!")
winner = True
elif user == 3 and computer == 1:
print("You lose! You played Scissors and I played Rock!")
winner = True
elif user == 3 and computer == 2:
print("You win! You played Scissors and I played Paper!")
winner = True
elif user == 1 and computer == 1:
print("Must play again! We both played Rock!")
elif user == 2 and computer == 2:
print("Must play again! We both played Paper!")
elif user == 3 and computer == 3:
print("Must play again! We both played Scissors!")
else:
print("Not a number.")
else:
print("game....over?")
gameloop()
I took the time to do an OOP Class example too! it could be optimized in many ways. However, I hope it shows you the next level of study! I also hope it helps you look into all the crazy design patterns and Game Programming Techniques you can apply as you learn.
import random
# we can create a player class to be used as an "interface" as we design the games logic
# this will let us scale the features be build in our game
# in this case i will leave it to you to add Wins to the scoreboard to help you learn
class player:
# your games requerment wants your players to make a choice from 1-3
choice = 0
# your games may requerment a player to be defined as the winner
win = 0
# your games may requerment a player to be defined as the losser
loss = 0
class game:
# by using classes and OOP we can scale the data and logic of your game
# here we create instances of the class player and define new objects based on your "requerments"
# your "requerments" where to have one Computer as a player, and one user as a player
computer = player()
user = player()
# this "function" will create a Scoreboard feature that can be called in the 'game loop' or in a future "event" of the game.
# Like a "Game Stats stage" at the end of the game
def Scoreboard(self, computer, user):
Computer = computer.loss
User = user.loss
print("+============= FINAL RESULTS: SCOREBOARD!! ======+ ")
print(" ")
print("Computer losses: ", Computer)
print("Player losses: ", User)
print(" ")
# Create a "function" that meets the requirements of a "game loop"
def main_loop(self, computer, user):
gameinput = input("Want to play Rock Paper Scissors? (Y/N) ")
if gameinput == "Y":
# Create a "while loop" to host the logic of the game.
# Each If statement will enable one "rule" of the game logic.
# game logic could be redesigned as an "Event".
# You can add a game "Event System" to your future project backlog
winner = False
while not winner:
print("1 = Rock, 2 = Paper, 3 = Scissors")
print('')
# we create 'Player1' as the user
Player1 = user
# we change the 'Player1' 'choice' to the user input
Player1.choice = int(
input("You have one chance of beating me. Input a number. "))
print('')
# we pull in to the game the computer player and call them 'Player1'
Player2 = computer
# we change the 'Player2' 'choice' to a random number
Player2.choice = random.randint(1, 3)
if user.choice == 1 and computer.choice == 2:
print("You lose! You played Rock and I played Paper!")
winner = True
user.loss += 1
elif user.choice == 1 and computer.choice == 3:
print("You win! You played Rock and I played Scissors!")
winner = True
computer.loss += 1
elif user.choice == 2 and computer.choice == 1:
print("You win! You played Paper and I played Rock!")
winner = True
computer.loss += 1
elif user.choice == 2 and computer.choice == 3:
print("You lose! You played Paper and I played Scissors!")
winner = True
user.loss += 1
elif user.choice == 3 and computer.choice == 1:
print("You lose! You played Scissors and I played Rock!")
winner = True
user.loss += 1
elif user.choice == 3 and computer.choice == 2:
print("You win! You played Scissors and I played Paper!")
winner = True
computer.loss += 1
elif user.choice == 1 and computer.choice == 1:
print("Must play again! We both played Rock!")
elif user.choice == 2 and computer.choice == 2:
print("Must play again! We both played Paper!")
elif user.choice == 3 and computer.choice == 3:
print("Must play again! We both played Scissors!")
else:
print("Not a number.")
# by returning "self" you call the same 'instances' of game that you will define below
return self.Scoreboard(user, computer)
else:
print("game....over?")
# define Instance of game as "test_game"
test_game = game()
# run game loop
test_game.main_loop()
I created the loss() , win() and draw() function to change the score following the events .The problem is that they are not getting called in the while loop to change the score and set it correctly. I had the score lines under every if and elif statement but decided to use a function to make the code shorter and more efficient.
from random import randint
print("Welcome to this game of rock paper scissors against the computer.")
print("Good luck and have fun! Enjoy this game! ")
print("To stop playing, type quit when the round ends.If you want to continue playing, press any key ")
scoreH=0
scoreAI=0
Round=0
def loss():
scoreH=scoreH+0
scoreAI=scoreAI+1
def win():
scoreH=scoreH+1
scoreAI=scoreAI+0
def draw():
scoreH=scoreH+0
scoreAI=scoreAi+0
x=True
while x == True :
# Choice
Round=Round+1
print("Round",Round,"is going to start. ")
print("1.Rock")
print("2.Paper")
print("3.Scissors")
choice=input("Enter your choice: ").lower()
IntChoice=randint(1,3)
#Transforming AI_choice from an int to a string
if IntChoice == 1:
AI_choice="rock"
elif IntChoice == 2:
AI_choice="paper"
elif IntChoice == 3:
AI_choice="scissors"
print(AI_choice)
# Draw
if choice==AI_choice:
draw()
print("The computer chose",choice,".You draw.")
# PlayerWins
elif choice=="paper" and AI_choice=="rock":
win()
print("The computer chose",choice,".You win.")
elif choice=="scissors" and AI_choice=="paper":
win()
print("The computer chose",AI_choice,".You win.")
elif choice=="rock" and AI_choice=="scissors":
win()
print("The computer chose",AI_choice,".You win.")
# AIwins
elif AI_choice=="paper" and choice=="rock":
loss()
print("The computer chose",AI_choice,".You lose.")
elif AI_choice=="scissors" and choice=="paper":
loss()
print("The computer chose",AI_choice,".You lose.")
elif AI_choice=="rock" and choice=="scissors":
loss()
print("The computer chose",AI_choice,".You lose.")
else:
print("Invalid input.")
continue
end=input()
# Stop Playing
if end == "stop" or end == "quit":
print("The score is :",scoreH,"-",scoreAI)
if scoreH > scoreAI:
print("You win.")
elif scoreH < scoreAI:
print("You lose.")
else:
print("You draw.")
break
else:
continue
They are indeed being called but they are modifying the local variable inside themselves.
To modify the values do something like this:
def loss(scoreAI):
return scoreAI+1
scoreAI = loss(scoreAI)
Actually, the functions are really simple and in some cases you're adding up 0 which is the same as doing nothing, so I wouldn't make functions for this. This is much simpler:
#Win
scoreH += 1
#Loss
scoreAI += 1
and you need nothing for draw.
I hope it helps!
import random
print("Welcome to this game of rock paper scissors against the computer.")
print("Good luck and have fun! Enjoy this game! ")
print("To stop playing, type quit when the round ends.If you want to continue playing, press any key ")
scoreH=0
scoreAI=0
Round=0
def loss():
global scoreH
global scoreAI
scoreH=scoreH+0
scoreAI=scoreAI+1
def win():
global scoreH
global scoreAI
scoreH=scoreH+1
scoreAI=scoreAI+0
def draw():
global scoreH
global scoreAI
scoreH=scoreH+0
scoreAI=scoreAI+0
x=True
while x == True :
# Choice
Round=Round+1
print("Round",Round,"is going to start. ")
print("1.Rock")
print("2.Paper")
print("3.Scissors")
choice=input("Enter your choice: ").lower()
IntChoice=random.randint(1,3)
#Transforming AI_choice from an int to a string
if IntChoice == 1:
AI_choice="rock"
elif IntChoice == 2:
AI_choice="paper"
elif IntChoice == 3:
AI_choice="scissors"
print(AI_choice)
# Draw
if choice==AI_choice:
draw()
print("The computer chose",choice,".You draw.")
# PlayerWins
elif choice=="paper" and AI_choice=="rock":
win()
print("The computer chose",choice,".You win.")
elif choice=="scissors" and AI_choice=="paper":
win()
print("The computer chose",AI_choice,".You win.")
elif choice=="rock" and AI_choice=="scissors":
win()
print("The computer chose",AI_choice,".You win.")
# AIwins
elif AI_choice=="paper" and choice=="rock":
scoreH,scoreAI=loss(scoreH,scoreAI)
print("The computer chose",AI_choice,".You lose.")
elif AI_choice=="scissors" and choice=="paper":
loss()
print("The computer chose",AI_choice,".You lose.")
elif AI_choice=="rock" and choice=="scissors":
loss()
print("The computer chose",AI_choice,".You lose.")
else:
print("Invalid input.")
#continue
end=input()
# Stop Playing
if end == "stop" or end == "quit":
x=False
print("The score is :",scoreH,"-",scoreAI)
if scoreH > scoreAI:
print("You win.")
elif scoreH < scoreAI:
print("You lose.")
else:
print("You draw.")
break
else:
continue
Expected Output:
Welcome to this game of rock paper scissors against the computer.
Good luck and have fun! Enjoy this game!
To stop playing, type quit when the round ends.If you want to continue playing, press any key
Round 1 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 2 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
paper
The computer chose paper .You draw.
Round 3 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: scissors
paper
The computer chose paper .You win.
Round 4 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 5 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: rock
scissors
The computer chose scissors .You win.
Round 6 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
scissors
The computer chose scissors .You lose.
Round 7 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice: paper
scissors
The computer chose scissors .You lose.
Round 8 is going to start.
1.Rock
2.Paper
3.Scissors
Enter your choice:
paper
Invalid input.
stop
The score is : 4 - 2
You win.
So I'm currently having some trouble with working out how to basically create a "count" function work within my Rock, Paper, Scissors game. I'm new to Python and this is my first foray into the use of multiple functions to execute game logic. Here's my code...
import random
from random import choice
cpu_score = 0
player_score = 0
# dictionary from which gestures are pulled
gestures = ["rock","paper","scissors"]
n_rounds = int(input("How many rounds would you like to play? "))
while n_rounds % 2 == 0 or n_rounds <= -1:
print("Number of rounds must be an odd number! Select an odd number!")
n_rounds = input("How many rounds?")
break
print("Lets play",n_rounds,"rounds!")
rounds_to_win = ((n_rounds + 1)//2)
rounds_to_win = round(rounds_to_win)
print("You must win",rounds_to_win,"rounds to beat the game!")
def computer_choice():
"""Movement made by the computer"""
comp = random.choice(gestures)
return comp
def player_gesture():
"""Movement by player"""
player = input("Please select, rock, paper or scissors")
if player not in gestures:
print("That's not an option! Please try again.")
player = input("Please select, rock, paper or scissors")
return player
def who_won_round(comp, player):
'''Who is the winner of the round.'''
winner = 0
if ((player == "rock") and (comp == "paper")) or \
((player == "paper") and (comp == "scissors")) or \
((player == "scissors") and (comp == "rock")):
winner = 1
elif ((comp == "rock") and (player == "paper")) or \
((comp == "paper") and (player == "scissors")) or \
((comp == "scissors") and (player == "rock")):
winner = 2
else:
winner = 0
return winner
def win_counter(winner, cpu_score,player_score):
rounds = 1
if winner == 1:
player_score += 1
print("This is round",rounds)
rounds += 1
return player_score
if winner == 2:
cpu_score += 1
print("This is round",rounds)
rounds += 1
return cpu_score
def count_round(winner, player, comp, cpu_score, player_score):
if winner == 0:
print("It's a tie!")
elif winner == 1:
print("The player chose",player)
print("The computer chose",comp)
print("The computer won the round!")
print("The computer has won",cpu_score,"rounds!")
elif winner == 2:
print("The player chose",player)
print("The computer chose",comp)
print("The player won the round!")
print("The player has won",player_score,"rounds!")
def game_structure():
while rounds_to_win < n_rounds:
comp = computer_choice()
player = player_gesture()
winner = who_won_round(comp,player)
win_count = win_counter(winner, cpu_score,player_score)
count = count_round(winner, player, comp, cpu_score, player_score)
game_structure()
Basically I'm having issues returning the variables in order to keep count of the scores of the "number of rounds" and "cpu_score" and "player_score". I prefer to not declare global variables as I realise they can be messy to use, but I'm not quite sure how to avoid this.
If you must avoid the use of global variables, you should take an object oriented approach. That way you can store the variables in the object.
So basically you do something like this:
newgame = mytictactoe()
while True #infinite loop
input("wanna play?")
if input == yes:
newgame.start
else:
break
From what I see, the cpu_score and player_score are never updated. You only have the newest result in win_count, that is not assigned to cpu_score or player_score at anytime. It could be something like:
win_count = win_counter(winner, cpu_score,player_score)
if winner == 1:
cpu_score = win_count
if winner == 2:
player_score = win_count
rounds_to_win = max(player_score, cpu_score)
count_round(winner, player, comp, cpu_score, player_score)
I did not run it, but this modifications should do the trick. There are better ways of doing it; maybe using a list to keep the scores and use winner as the index, or an object approach as someone else said, but I do not want to change too much the code.
Also, bear in mind that the round variable in win_counter does nothing.
Problems:
Program does not seem to accept the integers entered. Won't add to win/loss/draw count and does not display computer choice in debug mode
Basics Design of the Program:
Write a program that lets the user play the game of Rock, Paper, Scissors against the computer.
The program should work as follows.
A menu is displayed:
Score: 0 wins, 0 draws, 0 losses
(D)ebug to show computer's choice
(N)ew game
(Q)uit
If the user enters "Q" or "q" the program would end. "N" or "n" for a new game, "D" or "d" for debug mode, anything else would cause an error message to be displayed.
When a game begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet unless we are in "D"ebug mode.)
The user enters his or her choice of “1-rock”, “2-paper”, or “3-scissors” at the keyboard.
The computer's choice is displayed.
A winner is selected according to the following rules:
• If one player chooses rock and the other player chooses scissors, then rock wins.
(The rock smashes the scissors.)
• If one player chooses scissors and the other player chooses paper, then scissors wins.(Scissors cuts paper.)
• If one player chooses paper and the other player chooses rock, then paper wins.
(Paper wraps rock.)
• If both players make the same choice, the game is a draw.
Your program would keep a running total of the number of wins, loses and draws.
Re-display the menu and repeat the game loop.
My Program:
import random
def main():
continuing = "y"
win = 0
lose = 0
draw = 0
while continuing == "y":
print("Score:", win,"wins,", draw, "draws,", lose,"losses")
print("(D)ebug to show computer's choice")
print("(N)ew game")
print("(Q)uit")
choice = input(" ")
if choice == "n" or choice == "N":
win, draw, lose = playgame(win, draw, lose)
elif choice == "d" or choice == "D":
win, draw, lose = playgame2(win, draw, lose)
elif choice == "q" or choice == "Q":
break
def playgame(win, draw, lose):
computer = random.randint(1,3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
win += 1
elif computer == 3 and player == 1:
Score = "You won"
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
lose += 1
elif computer == player:
Score = "Draw"
draw += 1
return (win, draw, lose)
def playgame2(win, draw, lose):
computer = random.randint(1, 3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
print("Computer chose rock")
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
print("Computer chose rock")
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
print("Computer chose paper")
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
print("Computer chose paper")
win += 1
elif computer == 3 and player == 1:
Score = "You won"
print("Computer chose scissors")
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
print("Computer chose scissors")
lose += 1
elif computer == player:
Score = "Draw"
print("Computer chose the same as you")
draw += 1
return (win, draw, lose)
main()
I'm no Pythonista, but at a guess, input returns strings, and you'll need to convert to integer before comparing to the computer's int.
I also think you are missing a trick in DRYing up your code - you should be able to have a single playgame method, which takes an additional boolean parameter debugmode, which instead of calling print directly, calls an indirection, e.g.:
def debugPrint(debugString, debugMode)
if debugMode
print(debugString)
Hope this makes sense?
This would work in Python 2.x, but, not in Python 3.x
In Python 3.x, input() returns strings. Thus, the player's input would be of the form "1" or "2" or "3". Since 1 and "1" are different, the program will not execute any of the lines in the if and elif blocks in playgame() and playgame2().
Here is a Python 3.x example:
>>> a = input("Input = ")
Input = 1
>>> print a
SyntaxError: Missing parentheses in call to 'print'
>>> print(a)
1
>>> a
'1'
>>> type(a)
<class 'str'>
Thus, you should use i = int(input("Input = ")) wherever you want an integer input.
However, in Python 2.x, input() will take 1 as 1 itself and not as "1". But, when you want to type a string as an inpu, you will have to give the quotes also. Here is an exxample:
>>> a1 = input("Input = ")
Input = 1
>>> a1
1
>>> type(a1)
<type 'int'>
>>> #I want to input a string now:
>>> a2 = input("Input = ")
Input = string
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
a2 = input("Input = ")
File "<string>", line 1, in <module>
NameError: name 'string' is not defined
>>> a2 = input("Input = ")
Input = "string"
>>> a2
'string'
>>> type(a2)
<type 'str'>
>>> a3 = raw_input("Input = ")
Input = hello
>>> a3
'hello'
>>> type(a3)
<type 'str'>
>>>
In Python 2.x, the raw_input() function takes the input as a string.