how do I decrease a value of a variable in every if statement
Question:
I am new to python. I am creating rock, paper, scissor in python. I have created two variables called comp_life and life. If I or the computer will lose, it will decrease and store the value the two variables
I used life = life – 1 in every if statement I lose and comp = comp – 1 in every statement I won.
I expected that the value will be stored and will be applied in every if statement. But I think the value is stored in only that if statement, not other if statement.
import random
print ("welcome. I am bob the AI.nWe will play rock paper scissors. This is how we play rock paper scissorsnRock beat scissorsnScissors beat papernPaper beat paper rockn")
print ("you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win nn")
while True:
def rock_paper_scissors():
life = 3
comp_life = 3
print("1 = rockn2 = papern3 =scissorsnEnter")
user_choice = int(input("1, 2, 3n"))
if user_choice == 1:
name = "rock"
elif user_choice == 2:
name = "paper"
elif user_choice == 3:
name = "scissors"
if user_choice > 3 or user_choice < 1:
print("Invalid choice")
rock_paper_scissors()
print("You choosed ", name)
print("Bob's turn")
computer_choice = random.randint(1,3)
if user_choice == 1 and computer_choice == 2:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed paper")
print("you now have ", life, " lives")
elif user_choice == 2 and computer_choice == 1:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed rock")
print("computer now have ", comp_life, " lives")
elif user_choice == 1 and computer_choice == 1:
print("Draw")
elif user_choice == 2 and computer_choice == 2:
print("Draw")
elif user_choice == 1 and computer_choice == 3:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed scissors")
print("computer now have ", comp_life, " lives")
elif user_choice == 2 and computer_choice == 3:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed scissors")
print("you now have ", life, " lives")
elif user_choice == 3 and computer_choice == 1:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed rock")
print("you now have ", life, " lives")
elif user_choice == 3 and computer_choice == 2:
print("You win")
comp_life = comp_life - 1
print ("computer lost a life")
print("bob choosed paper")
print ("computer now have ", comp_life, " lives")
elif user_choice == 3 and computer_choice == 3:
print("Draw")
elif user_choice == 1 and computer_choice == 3:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed scissors")
print ("computer now have ", comp_life, " lives")
if life < 1:
print("You lose")
exit()
elif comp_life < 1:
print("You win")
exit()
rock_paper_scissors()
Answers:
I have simplified your function. Instead of using while True
, just use the recursive function, it will make sense to run a function that is called many times.
import random
print("welcome. I am bob the AI.nWe will play rock paper scissors. This is how we play rock paper scissorsnRock beat scissorsnScissors beat papernPaper beat paper rockn")
print("you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win nn")
def rock_paper_scissors(list_name, life, comp_life):
print("1 = rockn2 = papern3 =scissorsnEnter")
user_choice = int(input("1, 2, 3n"))
if user_choice > 3 or user_choice < 1:
print("Invalid choice")
rock_paper_scissors(list_name, life, comp_life)
name = list_name[user_choice - 1]
print("You choosed ", name)
print("Bob's turn")
computer_choice = random.randint(1, 3)
if user_choice == computer_choice:
print("Draw")
elif (user_choice == 1 and computer_choice == 3) or (user_choice > computer_choice):
print("You win")
comp_life -= 1
print("computer lost a life")
print("bob choosed", list_name[computer_choice - 1])
print("computer now have ", comp_life, " lives")
else:
print("You lose")
life -= 1
print("you lost a life")
print("bob choosed", list_name[computer_choice - 1])
print("you now have ", life, " lives")
print(f"=== You ({life}) vs Bob ({comp_life}) ===")
if life < 1:
print("You lose")
exit()
elif comp_life < 1:
print("You win")
exit()
else:
rock_paper_scissors(list_name, life, comp_life)
list_name = ["rock", "paper", "scissors"]
life = 3
comp_life = 3
rock_paper_scissors(list_name, life, comp_life)
Example output:
welcome. I am bob the AI.
We will play rock paper scissors. This is how we play rock paper scissors
Rock beat scissors
Scissors beat paper
Paper beat paper rock
you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
1
You choosed rock
Bob's turn
Draw
=== You (3) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
Draw
=== You (3) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
3
You choosed scissors
Bob's turn
You lose
you lost a life
bob choosed rock
you now have 2 lives
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
Draw
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
1
You choosed rock
Bob's turn
Draw
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
You lose
you lost a life
bob choosed rock
you now have 1 lives
=== You (1) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
3
You choosed scissors
Bob's turn
You lose
you lost a life
bob choosed paper
you now have 0 lives
=== You (0) vs Bob (3) ===
You lose
Your issue was primarily that you were resetting the counter each iteration of the loop, but there are many things that could be improved.
Here is another implementation for you to consider, with the following features:
- Uses a dictionary for choices for consistency.
- Uses separate recursive function for getting user input.
- Validates the input.
- Allows any number of starting lives.
- Should be pretty easy to follow.
import random
CHOICES = {1: "rock", 2: "paper", 3: "scissors"}
def get_choice() -> int:
choices_str = ", ".join([f"{k} for {v}" for k, v in CHOICES.items()])
choice = input(f"Enter {choices_str}: ")
if choice not in ["1", "2", "3"]:
choice = get_choice()
return int(choice)
def game(lives: int) -> bool:
"""Given starting lives play with computer and return bool indicating win."""
comp_lives = lives
player_lives = lives
while comp_lives > 0 and player_lives > 0:
print(f"Your lives: {player_lives}, Computer lives: {comp_lives}")
player_choice = get_choice()
print(f"You chose {CHOICES[player_choice]}.")
comp_choice = random.randint(1, 3)
print(f"Computer chose {CHOICES[comp_choice]}.")
if comp_choice == player_choice: # draw
continue # draw
if comp_choice in [player_choice + 1, player_choice - 2]: # computer win
player_lives -= 1
continue
if player_choice in [comp_choice + 1, player_choice - 2]: # player win
comp_lives -= 1
if comp_lives > 0:
print("Computer wins; you suck.")
return False
print("You win!")
return True
if __name__ == "__main__":
win = game(3)
Here is a sample interaction:
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
Your lives: 3, Computer lives: 2
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
Your lives: 3, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 3
You chose scissors.
Computer chose rock.
Your lives: 2, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 4
Enter 1 for rock, 2 for paper, 3 for scissors: 3
You chose scissors.
Computer chose rock.
Your lives: 1, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 1, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
You win!
As an aside, I completely understand if this is removed as "not an answer", but I still hope it is helpful.
I am new to python. I am creating rock, paper, scissor in python. I have created two variables called comp_life and life. If I or the computer will lose, it will decrease and store the value the two variables
I used life = life – 1 in every if statement I lose and comp = comp – 1 in every statement I won.
I expected that the value will be stored and will be applied in every if statement. But I think the value is stored in only that if statement, not other if statement.
import random
print ("welcome. I am bob the AI.nWe will play rock paper scissors. This is how we play rock paper scissorsnRock beat scissorsnScissors beat papernPaper beat paper rockn")
print ("you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win nn")
while True:
def rock_paper_scissors():
life = 3
comp_life = 3
print("1 = rockn2 = papern3 =scissorsnEnter")
user_choice = int(input("1, 2, 3n"))
if user_choice == 1:
name = "rock"
elif user_choice == 2:
name = "paper"
elif user_choice == 3:
name = "scissors"
if user_choice > 3 or user_choice < 1:
print("Invalid choice")
rock_paper_scissors()
print("You choosed ", name)
print("Bob's turn")
computer_choice = random.randint(1,3)
if user_choice == 1 and computer_choice == 2:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed paper")
print("you now have ", life, " lives")
elif user_choice == 2 and computer_choice == 1:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed rock")
print("computer now have ", comp_life, " lives")
elif user_choice == 1 and computer_choice == 1:
print("Draw")
elif user_choice == 2 and computer_choice == 2:
print("Draw")
elif user_choice == 1 and computer_choice == 3:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed scissors")
print("computer now have ", comp_life, " lives")
elif user_choice == 2 and computer_choice == 3:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed scissors")
print("you now have ", life, " lives")
elif user_choice == 3 and computer_choice == 1:
print("You lose")
life = life - 1
print("you lost a life")
print("bob choosed rock")
print("you now have ", life, " lives")
elif user_choice == 3 and computer_choice == 2:
print("You win")
comp_life = comp_life - 1
print ("computer lost a life")
print("bob choosed paper")
print ("computer now have ", comp_life, " lives")
elif user_choice == 3 and computer_choice == 3:
print("Draw")
elif user_choice == 1 and computer_choice == 3:
print("You win")
comp_life = comp_life - 1
print("computer lost a life")
print("bob choosed scissors")
print ("computer now have ", comp_life, " lives")
if life < 1:
print("You lose")
exit()
elif comp_life < 1:
print("You win")
exit()
rock_paper_scissors()
I have simplified your function. Instead of using while True
, just use the recursive function, it will make sense to run a function that is called many times.
import random
print("welcome. I am bob the AI.nWe will play rock paper scissors. This is how we play rock paper scissorsnRock beat scissorsnScissors beat papernPaper beat paper rockn")
print("you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win nn")
def rock_paper_scissors(list_name, life, comp_life):
print("1 = rockn2 = papern3 =scissorsnEnter")
user_choice = int(input("1, 2, 3n"))
if user_choice > 3 or user_choice < 1:
print("Invalid choice")
rock_paper_scissors(list_name, life, comp_life)
name = list_name[user_choice - 1]
print("You choosed ", name)
print("Bob's turn")
computer_choice = random.randint(1, 3)
if user_choice == computer_choice:
print("Draw")
elif (user_choice == 1 and computer_choice == 3) or (user_choice > computer_choice):
print("You win")
comp_life -= 1
print("computer lost a life")
print("bob choosed", list_name[computer_choice - 1])
print("computer now have ", comp_life, " lives")
else:
print("You lose")
life -= 1
print("you lost a life")
print("bob choosed", list_name[computer_choice - 1])
print("you now have ", life, " lives")
print(f"=== You ({life}) vs Bob ({comp_life}) ===")
if life < 1:
print("You lose")
exit()
elif comp_life < 1:
print("You win")
exit()
else:
rock_paper_scissors(list_name, life, comp_life)
list_name = ["rock", "paper", "scissors"]
life = 3
comp_life = 3
rock_paper_scissors(list_name, life, comp_life)
Example output:
welcome. I am bob the AI.
We will play rock paper scissors. This is how we play rock paper scissors
Rock beat scissors
Scissors beat paper
Paper beat paper rock
you and bob have three lives if you lose all lives the game will be over, if computer lose all lives you will win
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
1
You choosed rock
Bob's turn
Draw
=== You (3) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
Draw
=== You (3) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
3
You choosed scissors
Bob's turn
You lose
you lost a life
bob choosed rock
you now have 2 lives
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
Draw
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
1
You choosed rock
Bob's turn
Draw
=== You (2) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
2
You choosed paper
Bob's turn
You lose
you lost a life
bob choosed rock
you now have 1 lives
=== You (1) vs Bob (3) ===
1 = rock
2 = paper
3 =scissors
Enter
1, 2, 3
3
You choosed scissors
Bob's turn
You lose
you lost a life
bob choosed paper
you now have 0 lives
=== You (0) vs Bob (3) ===
You lose
Your issue was primarily that you were resetting the counter each iteration of the loop, but there are many things that could be improved.
Here is another implementation for you to consider, with the following features:
- Uses a dictionary for choices for consistency.
- Uses separate recursive function for getting user input.
- Validates the input.
- Allows any number of starting lives.
- Should be pretty easy to follow.
import random
CHOICES = {1: "rock", 2: "paper", 3: "scissors"}
def get_choice() -> int:
choices_str = ", ".join([f"{k} for {v}" for k, v in CHOICES.items()])
choice = input(f"Enter {choices_str}: ")
if choice not in ["1", "2", "3"]:
choice = get_choice()
return int(choice)
def game(lives: int) -> bool:
"""Given starting lives play with computer and return bool indicating win."""
comp_lives = lives
player_lives = lives
while comp_lives > 0 and player_lives > 0:
print(f"Your lives: {player_lives}, Computer lives: {comp_lives}")
player_choice = get_choice()
print(f"You chose {CHOICES[player_choice]}.")
comp_choice = random.randint(1, 3)
print(f"Computer chose {CHOICES[comp_choice]}.")
if comp_choice == player_choice: # draw
continue # draw
if comp_choice in [player_choice + 1, player_choice - 2]: # computer win
player_lives -= 1
continue
if player_choice in [comp_choice + 1, player_choice - 2]: # player win
comp_lives -= 1
if comp_lives > 0:
print("Computer wins; you suck.")
return False
print("You win!")
return True
if __name__ == "__main__":
win = game(3)
Here is a sample interaction:
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 3
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
Your lives: 3, Computer lives: 2
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
Your lives: 3, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 3, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 3
You chose scissors.
Computer chose rock.
Your lives: 2, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 4
Enter 1 for rock, 2 for paper, 3 for scissors: 3
You chose scissors.
Computer chose rock.
Your lives: 1, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose paper.
Your lives: 1, Computer lives: 1
Enter 1 for rock, 2 for paper, 3 for scissors: 2
You chose paper.
Computer chose rock.
You win!
As an aside, I completely understand if this is removed as "not an answer", but I still hope it is helpful.