how to increment a value using while loop python - python

could you tell me why my program doesnt increment "bot_points = 0"
"your_points = 0" to stop the while loop when some of these values reach 3
import random
rock_paper_scissors = ["ROCK", "PAPER", "SCISSORS"]
bot_points = 0
your_points = 0
while bot_points <= 3 or your_points <= 3:
user_choice = input('type "rock", "paper" or "scissors": ').upper()
bot_choice = random.choice(rock_paper_scissors)
if bot_choice == "ROCK" and user_choice == "ROCK":
print("Draw")
elif bot_choice == "ROCK" and user_choice == "PAPER":
print("You win")
your_points = your_points+1
elif bot_choice == "PAPER" and user_choice == "PAPER":
print("Draw")
elif bot_choice == "PAPER" and user_choice == "SCISSORS":
print("You win")
your_points= your_points+1
elif bot_choice == "PAPER" and user_choice == "ROCK":
print("You lose")
bot_points = bot_points+1
elif bot_choice == "SCISSORS" and user_choice == "PAPER":
print("You lose")
bot_points = bot_points+1
elif bot_choice == "SCISSORS" and user_choice == "ROCK":
print("You win")
your_points = your_points+1
elif bot_choice == "SCISSORS" and user_choice == "SCISSORS":
print("Draw")
elif bot_choice == "ROCK" and user_choice == "SCISSORS":
print("You lose")
bot_points = bot_points+1

Two issues:
You test for <= 3, so it won't end until someone wins four times
You test bot_points <= 3 or your_points <= 3, so it won't end until both players win at least four times; and would end when either reaches the threshold
If you want to end when either player wins three times, change the test to:
while bot_points < 3 and your_points < 3:

Related

Tricks and tips for beginners to shorten and simplify code

I am looking for ways to shorten and simplify my python code. Here is one example of one small rock/paper/scissors game I wrote earlier. The code kinda looks too long to me and I want to try to learn how to shorten my code.
import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")
while True:
user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
break
elif user_input not in options:
print("type in a valid word next time")
continue
game_limit += 1
if game_limit == 5 and comp_wins > user_wins:
print("The game is over, YOU LOST!")
elif game_limit == 5 and comp_wins < user_wins:
print("The game is over, YOU WON!")
random_number = random.randint(0,2)
comp_input = options[random_number]
print("Computer picked", comp_input)
if user_input == "rock" and comp_input == "scissors":
print("You win")
user_wins += 1
elif user_input == "rock" and comp_input == "rock":
print("its a draw")
elif user_input == "rock" and comp_input == "paper":
print("You lose!")
comp_wins += 1
if user_input == "scissors" and comp_input == "paper":
print("You win")
user_wins += 1
elif user_input == "scissors" and comp_input == "scissors":
print("its a draw")
elif user_input == "scissors" and comp_input == "rock":
print("You lose!")
comp_wins += 1
if user_input == "paper" and comp_input == "rock":
print("You win")
user_wins += 1
elif user_input == "paper" and comp_input == "paper":
print("its a draw")
elif user_input == "paper" and comp_input == "scissors":
print("You lose!")
comp_wins += 1
Yes. You could simplify your logic by having compound and/or tests for the player and user. Following is a revised version of your code with some simplified logic.
import random
user_wins = 0
comp_wins = 0
game_limit = 0
options = ["rock","paper","scissors"]
print("Welcome to Rock Paper Scissors, This is a Best of 5")
while True:
if game_limit >= 5: # Tweak
if comp_wins > user_wins:
print("The game is over, YOU LOST!")
break
if comp_wins < user_wins:
print("The game is over, YOU WON!")
break
else:
print("We go to sudden death!")
user_input = input("Type in Rock/Paper/Scissors or Q to quit: ").lower()
if user_input == "q":
break
elif user_input not in options:
print("type in a valid word next time")
continue
game_limit += 1
random_number = random.randint(0,2)
comp_input = options[random_number]
print("Computer picked", comp_input)
if user_input == "rock" and comp_input == "scissors" or user_input == "paper" and comp_input == "rock" or user_input == "scissors" and comp_input == "paper":
print("You win")
user_wins += 1
elif user_input == "rock" and comp_input == "paper" or user_input == "paper" and comp_input == "scissors" or user_input == "scissors" and comp_input == "rock":
print("You lose!")
comp_wins += 1
else:
print("It's a draw")
This way all of the scenarios where the user can win are in one logical test and all of the scenarios where the computer can win are in another logical test. If neither test is true, then the default would have to be a draw.
Also, I tweaked game limit test because if the computer and user have the same score when the game limit is reached, the test result would be false and the game would then continue on and not stop. So there is a bit of additional code to handle a tie after five rounds.
Give that a try.

The terminal says that computer_choice doesn’t exist even though it does

WHile I was attempting a simple rock, paper scissors, I ran into this proble. When I execute the file and input the user_input, it says to me that the variable computer_choice doesn’t exist, even though it does exist. I would appreciate if someone could help me, thank you.
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit. ").lower()
if user_input == "q":
break
if user_input not in options:
continue
random_number = random.randint (0, 2)
computer_choice = options[random_number]
print ("The computer picked:", computer_choice + ".")
if user_input == "rock" and computer_choice == "scissors" :
print("You Won!")
user_wins += 1
continue
elif user_input == "scissors" and computer_choice == "paper" :
print("You Won!")
user_wins += 1
continue
elif user_input == "paper" and computer_pick == "rock" :
print("You Won!")
user_wins += 1
continue
else:
print("You Lost!")
computer_wins += 1
continue
print("You won", user_wins,"times and the computer won", computer_wins, "times.")
print("Goodbye")
You have to be careful with indentation when using python:
import random
user_wins = 0
computer_wins = 0
options = ["rock", "paper", "scissors"]
while True:
user_input = input("Type Rock/Paper/Scissors or Q to quit. ").lower()
if user_input == "q":
break
if user_input not in options:
continue
# not at this level
# at this level
random_number = random.randint(0, 2)
computer_choice = options[random_number]
print("The computer picked:", computer_choice + ".")
if user_input == "rock" and computer_choice == "scissors":
print("You Won!")
user_wins += 1
continue
elif user_input == "scissors" and computer_choice == "paper":
print("You Won!")
user_wins += 1
continue
elif user_input == "paper" and computer_choice == "rock": #not computer_pick
print("You Won!")
user_wins += 1
continue
Also you wrote computer_pick in line 31, probably you mean computer_choice

How do you have the computer change its choice depending on the input of the player in this game of rock, paper, scissors?

I want the computer to follow a win-stay, lose-switch strategy but I can't seem to get the computer to change its option from rock. Please help!
import random
def game():
game_start
computer_count = 0
user_count = 0
while True:
base_choice = ['scissors', 'paper', 'rock']
computer_choice = 'rock'
user_choice = input('(scissors, paper, rock) Type your choice: ').strip().lower()
print()
computer_wins = 'The computer wins!'
you_win = 'You win!'
print(f'You played {user_choice}, the computer played {computer_choice}')
if user_choice == 'scissors' and computer_choice == 'rock':
computer_choice = 'rock'
elif user_choice == 'paper' and computer_choice == 'scissors':
computer_choice = 'scissors'
elif user_choice == 'rock' and computer_choice == 'paper':
computer_choice = 'paper'
print(computer_wins)
computer_count += 1
if user_choice == 'rock' and computer_choice == 'scissors':
computer_choice = 'paper'
elif user_choice == 'scissors' and computer_choice == 'paper':
computer_choice = 'rock'
elif user_choice == 'paper' and computer_choice == 'rock':
computer_choice = 'scissors'
print(you_win)
user_count += 1
else:
if user_choice == computer_choice:
print('Its a draw!')
computer_count += 0
user_count += 0
print(f'Computer: {computer_count} - You: {user_count}')
print()
game()
It's because you put computer_choice = 'rock' inside the while loop. Having inside causes it to be reset to rock for every play.
Another issue is that your if statements only prints who wins and increases count for the last one.
You can also compress the if statements a little, but that wasn't part of the question, so I didn't do it here.
game_start
computer_count = 0
user_count = 0
def game():
computer_choice = 'rock'
while True:
#base_choice = ['scissors', 'paper', 'rock']
# no point in having an unused list
user_choice = input('(scissors, paper, rock) Type your choice: ').strip().lower()
print()
computer_wins = 'The computer wins!'
you_win = 'You win!'
print(f'You played {user_choice}, the computer played {computer_choice}')
if user_choice == 'scissors' and computer_choice == 'rock':
computer_choice = 'rock'
print(computer_wins)
computer_count += 1
elif user_choice == 'paper' and computer_choice == 'scissors':
computer_choice = 'scissors'
print(computer_wins)
computer_count += 1
elif user_choice == 'rock' and computer_choice == 'paper':
computer_choice = 'paper'
print(computer_wins)
computer_count += 1
if user_choice == 'rock' and computer_choice == 'scissors':
computer_choice = 'paper'
print(you_win)
user_count += 1
elif user_choice == 'scissors' and computer_choice == 'paper':
computer_choice = 'rock'
print(you_win)
user_count += 1
elif user_choice == 'paper' and computer_choice == 'rock':
computer_choice = 'scissors'
print(you_win)
user_count += 1
else:
if user_choice == computer_choice:
print('Its a draw!')
# no point in adding 0
print(f'Computer: {computer_count} - You: {user_count}')
print()
game()
In response to your making it random comment, you need to import the random module.
Here, the base_choice is used, so uncomment it and it'll work:
import random #place at beginning of code
# can instead do `from random import choice`
base_choice = ['scissors', 'paper', 'rock']
# needs to be before you use it in random.choice
# to get random choice, do
computer_choice = random.choice(base_choice)

Rock, Papers and Scissors in Python

(I am new to python, please don't judge the quality of the code right now.)
So, I am making a program which let's you play rock, papers and scissors with the code. This is my code:
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
elif computer_choice == "scissors":
print(f"{name} wins!")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
if computer_choice == "rock":
print(f"{name} wins!")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
elif computer_choice == "papers":
print(f"{name} wins!")
if user_choice == computer_choice:
print("Tie!")
But, like when I run the code, it is working out really good. However, the problem is that if the user does not enter the correct spelling, the output looks really messed up. Like this:
Enter your name:
_____
Hi _____, choose between rock, papers and scissors
> asdf
ERROR
Computer chooses: rock
I do not want the last line, the one which says "Computer chooses: rock". Please someone suggest how do I go about this.
Thanks in advance 🙏
(EDIT: Also, if you can suggest how I can include a point system in this, which calculates how many times the computer wins, the user wins and how many times the match was a tie, the effort would be really appreciated.)
Just use continue to force the user to enter a valid choice:
while True:
user_choice = str(input('> '))
user_choice = user_choice.lower()
if user_choice not in choices:
print(f"You chose {user_choice}, please choose rock, paper, or scissors")
continue
[...]
Looks like your error problem has already been solved : )
But for the tie and score counter I have prepared the following code-
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
Cwin = 0
Userwin = 0
Tie = 0
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "scissors":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "rock":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
Cwin = Cwin + 1
print(int(Cwin) , "Computer victories so far")
elif computer_choice == "papers":
print(f"{name} wins!")
Userwin = Userwin + 1
print(int(Userwin) , "User victories so far")
if user_choice == computer_choice:
print("Tie!")
Tie = Tie + 1
print(int(Tie) , "Ties so far")
Look here the screenshot of it working properly-
Just add computer choose inside else:
import random
name = str(input("Enter your name:\n"))
choices = ["rock", "papers", "scissors"]
print(f"Hi {name}, choose between rock, papers and scissors")
while True:
user_choice = str(input('> '))
user_choice.lower()
if user_choice not in choices:
print("ERROR")
else:
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == "rock":
if computer_choice == "papers":
print("Computer wins!")
elif computer_choice == "scissors":
print(f"{name} wins!")
if user_choice == "papers":
if computer_choice == "scissors":
print("Computer wins!")
if computer_choice == "rock":
print(f"{name} wins!")
if user_choice == "scissors":
if computer_choice == "rock":
print("Computer wins!")
elif computer_choice == "papers":
print(f"{name} wins!")
if user_choice == computer_choice:
print("Tie!")
Here is a possible implementation:
import random
name = str(input("Enter your name: "))
choices = ["rock", "scissors", "paper"]
print(f"Hi {name}, choose between rock, paper and scissors")
while True:
user_choice = None
while user_choice not in choices:
user_choice = str(input('> '))
user_choice.lower()
computer_choice = random.choice(choices)
print(f"Computer chooses: {computer_choice}")
if user_choice == computer_choice:
print("Tie!")
elif choices[(choices.index(user_choice) + 1) % len(choices)] == computer_choice:
print(f"{name} wins!")
else:
print(f"Computer wins!")
Some insights:
You can accomplish the comparing mechanism with a rounded list, and not explicit comparison.
Do a while loop for the user input until it is correct.

restarting while loop in a game

I'm trying to write a rock-paper-scissors game in python.this is the code:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
play = False
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while play == False:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ",computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
break
again = input("do you want another round?:")
if again == "yes":
play = False
else:
play = True
the program works well but I want it to ask the player whether or not he/she wants to do another round.If the answer is yes the program must restart the loop.
The problem is I don't know how to do that.I know it's probably related to True and False and I attempted to do something as you can see in the code but it didn't work.
please Help me.
A simple fix might be just putting your while loop as True and continuing to loop until you break the execution:
D_0 = {1: "rock", 2: "scissors", 3: "paper"}
from random import randint
name = input("What is your name?: ")
print("%s you have to pick among rock,paper and scissors." % name)
while True:
p_1 = input("which one?")
computer = D_0[randint(1, 3)]
print("my choice is: ", computer)
if p_1 == computer:
print("it's a draw")
elif p_1 == "scissors" and computer == "paper":
print("you won!")
elif p_1 == "paper" and computer == "scissors":
print("you lost!")
elif p_1 == "scissors" and computer == "rock":
print("you lost!")
elif p_1 == "rock" and computer == "paper":
print("you lost!")
elif p_1 == "rock" and computer == "scissors":
print("you won!")
elif p_1 == "paper" and computer == "rock":
print("you won!")
else:
print("Invalid input")
again = input("do you want another round?:")
if again != "yes":
break

Categories