Question:
It seems that every time the Computer and the player reach a tie, the score still shows as 1 for the player when it is supposed to be 0 for both, also how can I add a error message that does not let you type anything else besides the given choices. Thank you for the help
code snippet
import random
print("Thank You for choosing to play Rock, Paper, Scissors UPGRADED. Welcome to Asteroid, Super Blanket, Sword! Choose your move!")
while True:
Player_score=0
Computer_score=0
user = input("Asteroid, Super Blanket, or Sword?")
computer = random.choice(["Asteroid","Super Blanket", "Sword"])
if user == computer:
print("You and the AI tied!")
elif user == "Super Blanket":
if computer == "Sword":
print("AI chose the Sword, The Sword cuts the Super Blanket, You Lose!")
Computer_score= +1
else:
print("AI chose the Asteroid, The Super Blanket wraps up the Asteroid, You Win!")
Player_score = +1
elif user == "Sword":
if computer == "Asteroid":
print("AI chose the Asteroid, The Asteroid crushes the Sword, You Lose!")
Computer_score= +1
else:
print("AI chose the Super Blanket, The Sword cuts the Super Blanket, You Win!")
Player_score = +1
elif user == "Asteroid":
if computer == "Super Blanket":
print("AI chose the Super Blanket, The Super Blanket wraps up the Asteroid, You Lose!")
Computer_score= +1
else:
print("AI chose the Sword, The Asteroid crushes the Sword, You Win!")
Player_score = +1
num_games = 1
for i in range(num_games):
print("Your score:", Player_score)
print("Computer score:", Computer_score)
Okay, Unary operations are allowed on Python, and, and therefore it is valid code, but @DominicOrtillan probably meant to add the value to the variable instead of setting it +1
Please keep your code style consistent Here are some good practices:
var = 5
# no + in front of it, one space before = and one space after
computer_score = 0
num_games = 0
# consistent capitalization
print('first line')
print('second line')
# large spaces wont look the same on all devices
print('first line\nsecond line')
# this works too, use \n for a linebreak
while True:
Player_score=0
Computer_score=0
user = ââ
while not user in ["Asteroid","Super Blanket", "Sword"]:
user = input("Asteroid, Super Blanket, or Sword?")
computer = random.choice(["Asteroid","Super Blanket", "Sword"])
(Your rest of the codes here)
This forces the user to type either one of them
If you want an error you can add an if statement
user = ââ
while not user in ["Asteroid","Super Blanket", "Sword"]:
user = input("Asteroid, Super Blanket, or Sword?")
if not user in ["Asteroid","Super Blanket", "Sword"]:
print("please enter a correct choice")
computer = random.choice(["Asteroid","Super Blanket", "Sword"])
I would recommend creating a function to do this for you. It makes the code repeatable and removes clutter.
def get_input(prompt, options, err=None):
user = input(prompt)
while user not in options:
print(err or "That was not an option!")
user = input(prompt)
return user
And now you can simple use it anywhere like this.
user_choice = get_input("Choose Asteroid, Super Blanket, or Sword", ["Asteroid", "Super Blanket", "Sword"])
And as for the err option, it allows you to have a custom error message when you put in the wrong input like this.
user_choice = get_input("Choose Asteroid, Super Blanket, or Sword", ["Asteroid", "Super Blanket", "Sword"], err="Please choose Asteroid, Super Blanket, or Sword")
def get_input(prompt: str, options: list | tuple, error: str="That was not an option!") -> str:
user_answer = input(prompt)
while user_answer not in options:
print(error)
user_answer = input(prompt)
return user
from time import sleep
def get_input(prompt: str, options: set[str], error: str = "Not an option!") -> str:
print(end=f"{prompt}\x1b7") # Save cursor position
while True:
response = input().title()
if response in options:
return response
print(end=f"\x1b[31m{error}\x1b[m", flush=True)
# Wait, so user can read error
sleep(0.07 * len(error))
# Clear error, restore cursor position
print(end="\x1b[2K\x1b8")
get_input("Foo or bar?", {"Foo", "Bar"})
set because the item lookup is faster, and I donât see a scenario where youâd need to pass an (ordered) list into this function.
Types arenât just for people reading the code directly. mypy, a static type checker, wonât accept or. And list or tuple â list, so this would happen: