Sometimes my code doesn't work and sometimes it does - python

I am creating a rock,paper,scissors game but sometimes my output shows and sometimes it doesn't what's seems to be the problem here?
So far this is my code:
import random
round = 1
win = 0
lose = 0
tie = 0
while True:
choices = ["rock", "paper", "scissors"]
computer = random.choice(choices)
player = None
while player not in choices:
player = input("Rock, paper, or scissors??: ").lower()
if player == computer:
print("Player : ", player)
print("Computer: ", computer)
print("Tie!")
tie+=1
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "paper":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "rock":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "paper":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
play_again = input("Would you like to play again? (yes/no): ").lower()
if play_again != "yes":
print("ROUNDS PLAYED: ",round)
print("TOTAL WINS:",win)
print("TOTAL LOSES: ",lose)
print("TOTAL TIME PLAYER TIED WITH COMPUTER: ",tie)
break
elif play_again == "yes":
rounds = round + 1
print("NEW ROUND, ROUND: ",rounds)
print("Bye, Thanks for playing this program")
now if I were to choose paper it won't work it will show me something like this:
(also I have realized that my rounds doesn't count.)
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: scissors
Player : scissors
Computer: scissors
Tie!
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: rock
Player : rock
Computer: rock
Tie!
Would you like to play again? (yes/no): yes
NEW ROUND, ROUND: 2
Rock, paper, or scissors??: paper
Would you like to play again? (yes/no): no
ROUNDS PLAYED: 1
TOTAL WINS: 0
TOTAL LOSES: 0
TOTAL TIME PLAYER TIED WITH COMPUTER: 2
Bye, Thanks for playing this program
I am new to programming any help would be highly appreciated.

You have two different elif block with same conditions. For Example:
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose += 1
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win += 1
In this case, if player choose scissors what is gonna happen?
Python starts to search for this condition but only the first one will be valid.
Thus, you should construct your elif block like this:
elif player == "scissors":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win += 1
elif computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose += 1
Let's look at your second question.
You have a variable named round and rounds. These are same I guess but you are incrementing rounds by the line rounds = round+1.
Instead, try
round = round+1
or
round+=1
Hope this helps.

Your code is not working sometimes because you have not applied all the condition for example what if player choses paper and computer choses rock.
Also the number of round is not increasing because you are saving it in new variable every time, hence not increasing .
rounds = round + 1
above line of code should be
round = round + 1

If you put the same condition more than once in any if-elif structure, only the first conditional if/elif branch will satisfy and the latter will never be executed. For example, in your code in the if-else statement, you have written the same condition elif player == "rock" twice.
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "paper":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
elif player == "scissors":
if computer == "rock":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif player == "rock":
if computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
Say, your player has entered rock and computer has chosen scissors. When the code executes, only the first elif player == "rock" will be evaluated, inside this statement, you only checked if the computer has chosen paper or not. But computer has chosen scissors which you did not check in this statement. So, the interpreter will do nothing and skip the rest of the if-else condition.
To make it work, you need to use only one else statement for the same condition, like this:
elif player == "rock":
if computer == "paper":
print("Player : ", player)
print("Computer: ", computer)
print("You lose")
lose+=1
elif computer == "scissors":
print("Player : ", player)
print("Computer: ", computer)
print("You win")
win+=1
You need to follow this for all the possible outcomes.

Related

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()

Python while True positioning confusion

I have made a rock paper scissors program as I am just beginning but I have discovered that the 2 positions I tried putting a while True: command have completely opposite effects. I can't understand the logic/reasoning behind it and was hoping someone would be able to explain it in simple terms.
the first copy of the code is the one that functions properly with the while True: on the 3rd line.
the second version of the code is what I had done first with the while True: on the 6th line.
the second version results in the result of the game being printed endlessly so E.G. "You win" a million times whereas when the while True: is on the 3rd line it runs as intended with the win/lose or tie message only printing once.
Can someone explain why that is just so I understand for future reference.
import random
moves = "rock", "paper", "scissors"
while True:
computer = moves[random.randint(0,2)]
player = input("Choose rock, paper or scissors... Or 'end' to end the game: ")
if player == "end":
print("Game over!")
break
elif player == "rock" and computer == "scissors" or player == "scissors" and computer == "paper" or player == "paper" and computer == "rock":
print("You win", player, "beats", computer)
elif player == computer:
print("Tie!")
else:
print("You lose!", computer, "beats", player)
import random
moves = "rock", "paper", "scissors"
computer = moves[random.randint(0,2)]
player = input("Choose rock, paper or scissors... Or 'end' to end the game: ")
while True:
if player == "end":
print("Game over!")
break
elif player == "rock" and computer == "scissors" or player == "scissors" and computer == "paper" or player == "paper" and computer == "rock":
print("You win", player, "beats", computer)
elif player == computer:
print("Tie!")
else:
print("You lose!", computer, "beats", player)

How can I store a number while using multiple if statements?

I am creating a python code for rock, paper, scissors but I can't seem to keep track of the score of wins, losses, and ties. I think there is something wrong with my "count" which i called "win += 0", "lose += 0" and "tie += 0." Could someone please tell me what I should do to increase the score by 1 every time there is a win, lose or tie?
Here is my code below:
from random import randint
t = ["rock", "paper", "scissors"]
computer = t[randint(0,2)]
player = False
lose = 0
win = 0
for i in range(0,10):
print("1... 2... 3... go!")
while player == False:
player = input("rock, paper, scissors: ")
print("Computer: ", computer)
print("User: ", player)
if player == computer:
tie = 0
tie +=1
print("Tie!")
elif player == "rock":
if computer == "paper":
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
elif player == "paper":
if computer == "scissors":
lose = 0
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
elif player == "scissors":
if computer == "rock":
lose +=0
print("You lose!")
else:
win +=0
print("You win!")
else:
print("That's not a valid play. Check your spelling!")
player = False
computer = t[randint(0,2)]
break
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)
if tie > win and tie > lose:
print("It's a tie!")
elif win > tie and win > lose:
print("You won!")
else:
print("The computer won!")
Here's the fixed version. I suggest you work on it some more :)
from random import choice
t = ["rock", "paper", "scissors"]
tie = 0
lose = 0
win = 0
for i in range(0, 10):
print("1... 2... 3... go!")
# you need to refresh these variables on every for iteration
computer = choice(t)
player = None
# if you're using while to make sure player inputs, that's the only thing that needs
# to be within the while loop
while not player:
player = input("rock, paper, scissors: ")
print("Computer: ", computer)
print("User: ", player)
# I would look for a way to simplify determining the winner
if player == computer:
# tie += 1 is the same as tie = tie + 1
tie +=1
print("Tie!")
elif player == "rock":
if computer == "paper":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
elif player == "paper":
if computer == "scissors":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
elif player == "scissors":
if computer == "rock":
lose += 1
print("You lose!")
else:
win += 1
print("You win!")
else:
print("That's not a valid play. Check your spelling!")
print("Final Tally")
print("************")
print("User Wins: ", win)
print("Computer Wins: ", lose)
print("Ties: ", tie)
if tie > win and tie > lose:
print("It's a tie!")
elif win > tie and win > lose:
print("You won!")
else:
print("The computer won!")
UPDATE: Apparently I have nothing to do. So ok, here's a straightforward way to simplify win conditioning.
win_condition_rock = player == 'rock' and computer == 'scissors'
win_condition_paper = player == 'paper' and computer == 'rock'
win_condition_scissors = player == 'scissors' and computer == 'paper'
if player == computer:
# tie += 1 is the same as tie = tie + 1
tie +=1
print("Tie!")
elif any([win_condition_paper, win_condition_scissors, win_condition_rock]):
win += 1
print('You win')
else:
lose += 1
print('You lose')
UPDATE 2: And here's a check for valid input
while player not in t:
player = input("rock, paper, scissors: ").lower()
if player not in t:
print('Invalid input')

Python score counter and lives in game with while, if, elif anf else

I'm trying to create this game but I'm stuck with this whole score counter lives and how to break the while loop.
player = input("Welcome! Let's play Rock, Paper, Scissors, Lizard, Spock. You have five lives. Press enter to start.")
from random import randint
t = ["rock", "paper", "scissors", "lizard", "spock"]
computer = t[randint(0,4)]
player = False
while player == False:
lives = 5
score = 0
player = input("rock, paper, scissors, lizard, spock?")
print("Computer plays",computer)
if player == computer:
print("Tie!")
score + 1
elif player == "rock":
if computer == "paper":
print("You lose!", computer, "covers", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "lizard":
print("You win!", player, "crushes", computer)
score + 2
elif computer == "spock":
print("You lose!", computer, "vaporises", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
else:
print("You win!", player, "smashes", computer)
score + 2
elif player == "paper":
if computer == "scissors":
print("You lose!", computer, "cuts", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "lizard":
print("You lose!", computer, "eats", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "spock":
print("You win!", player, "disproves", computer)
score + 2
else:
print("You win!", player, "covers", computer)
score + 2
elif player == "scissors":
if computer == "rock":
print("You lose...", computer, "smashes", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "spock":
print("You lose!", computer, "smashes", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "lizard":
print("You win!", player, "decapitates", computer)
score + 2
else:
print("You win!", player, "cuts", computer)
score + 2
elif player == "spock":
if computer == "lizard":
print("You lose!", computer, "poisons", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "scissors":
print("You win!", player, "smashes", computer)
score + 2
elif computer == "rock":
print("You win!", player, "vaporises", computer)
score + 2
else:
print("You lose!", computer, "disproves", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif player == "lizard":
if computer == "spock":
print("You win!", player, "poisons", computer)
score + 2
elif computer == "rock":
print("You lose!", computer, "crushes", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
elif computer == "paper":
print("You win!", player, "eats", computer)
score + 2
else:
print("You lose!", computer, "decapitates", player)
lives - 1
if lives == 0:
print("Your socre is", score)
break
else:
print("That's not a valid play!")
player = False
computer = t[randint(0,4)]
A few suggestions to get your expected behavior of managing the score counter, lives and how to break the while loop.
Move score and lives above the while-loop.
Even though you are calculating their new values within the loop, their values will reset each loop if they are defined within the loop.
Use increment and decrement operators for calculations.
When doing calculations to assign new values, you'll want to save the result of the calculation back to the variable. Statements like score + 1 and lives - 1 have no effect on those variables until the result gets saved. You could change all score + 1 to score = score + 1 (or score +=1), and similarly change all lives - 1 to lives = lives - 1 (or lives -=1). See Behaviour of increment and decrement operators in Python for more detail.
Remove all break statements.
break is useful if and when you want to break out of the while loop, but since the game keeps playing (I assume, since you keep track of lives) until all lives are lost, then the loop will "break" when the condition lives > 0 is False.
After making these changes, your game should behave as expected.
Hope this helps.
BTW, good job on the game, I enjoyed playing and debugging it!

TypeError: input expected at most 1 arguments, got 3 (Rock, paper, scissors game)

Keep getting this syntax error
TypeError: input expected at most 1 arguments, got 3
Does anyone know how to fix this?
from random import randint
from tkinter import *
po = ["Rock", "Paper", "Scissors"]
player = False
cpu = po[randint(0, 2)]
while player == False:
player = input("Rock", "Paper", "Scissors?")
if player == computer:
print("Tie")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cuts", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose!", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!")
player = False
computer = po[randint(0,2)]
You're misusing input. The argument passed to it is merely the prompt presented, and thus can only be a single string.
player = input("Rock, Paper, Scissors?")
Is probably more like what you want.
It seems that you never really give the person a chance to input wether rock, paper, or scissors, you should probably do something along the lines of
player = str(input("Rock, Paper, or Scissors?"))
That way player is assigned to whichever the player chooses

Categories

Resources