Help with Code please

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

Repl link:
https://replit.com/@DominicOrtillan/RoPaSc#main.py

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)
2 Likes

If I look at the elke in your code, I guess you meant to have Player_score = +1 in there and not outside of it as it is now.

 else:
    print("AI chose the Sword, The Asteroid crushes the Sword, You Win!") 
    Player_score = +1

also you means = +1 or += 1?

3 Likes

I think Player_score = +1 is a weird way to assign variables. It’s either Player_score += 1 or Player_score = 1.

3 Likes

Why wouldn’t Python allow unary operations? You can say -1, so wouldn’t you be able to say +1?

2 Likes

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

1 Like

Please keep your code style consistent :pleading_face: 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
2 Likes

Well you can use another looping, go

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"])
1 Like

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

Change that to

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
1 Like

You can do this with the typing library like this. :upside_down_face:

from typing import Union

options: Union[list, tuple]

there is no “correct way” when it comes to typing… as long as you and anyone else editing the code understands, then it’s fine.

2 Likes

Here’s my take

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:

>>> get_input.__annotations__["options"]
<class 'list'>

Here, the correct way is now options: list | tuple which I think is more readable as well.

2 Likes