r/code • • Sep 20 '21

Halp pls! -Python-I’m trying to make it so the code always beats the input. I keep getting an error. Any ideas on what I’m doing wrong?

14 Upvotes

7 comments sorted by

3

u/14446368 Sep 20 '21 edited Sep 20 '21

You need to be using "elif:" and make sure your indents are correct.

In your second image, you seem to be using the "walrus" operator questionably, and you don't have quotes around the right side of the operator, meaning you're comparing to a variable that likely isn't defined.

The way I'd do it (simply, quickly) would be:

answer = input("Rock, Paper, or Scissors?")
if answer == "Rock":
    print("Paper")
elif: answer == "Paper":
    print("Scissors")
else:
    print("Rock")
print("Haha! I win! And definitely without any cheating!")

Keep in mind this doesn't catch things like misspelling or even having different case. If I put "paper" into the input here, the above would spit out "Rock," whereas "Paper" (capital P) would hit "Scissors."

5

u/abittoxic24 Sep 20 '21

Changed the code up a little bit, but I got it to work now with what you wrote as a base so thank you!

2

u/14446368 Sep 20 '21

You're welcome! Welcome to a brave new Hello, World!

3

u/abittoxic24 Sep 20 '21

Thank you, I’ll try this now!

1

u/Lunabotics Sep 20 '21

I would do it a bit differently. Since the game is simple enough you could put all answers in a dictionary. As far as a bug, you're colon after the variable doesn't make sense and 14446368 already answered the syntax problem.

game_over = False
winning_play = {'Rock': 'Paper', 'Paper': 'Scissors', 'Scissors': 'Rock' }
while not game_over:
    answer = input("Rock, Paper or Scissors")    
    if answer in winning_play:
        print(winning_play[answer])
        game_over = True
    else:
        print("Please try again")

1

u/kurti256 Sep 21 '21

Just to add on a dictionary is just a list where each part of the list (called an element) is labelled so the first element here is labelled with the string rock so if you did

Print(winning play['Rock'])

Your output would be Paper

1

u/[deleted] Sep 21 '21

You have the colon after name, but you should have it after “Rock”.

if name = “Rock”: …