I recently started learning python, and as an exercise, decided to make a simple tic-tac-toe game. However, when I try to load it, it just hangs. At first, I figured it was a problem with my editor, but I tried uploading it onto online editors, and it keeps giving the same error at line 12 (the first instance of using string literal method). The code is as follows:
from random import randint
hum_score = 0
comp_score = 0
while hum_score and comp_score != 3:
hum_decision = input("rock, paper, or scissor?")
comp_options = ['rock', 'paper', 'scissor']
comp_decision = comp_options[randint(0,2)]
if hum_decision == 'rock' and comp_decision == 'paper':
comp_score += 1
print(f"Computer chose paper, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'rock' and comp_decision == 'scissor':
hum_score += 1
print(f"Computer chose scissor, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'paper' and comp_decision == 'scissor':
comp_score += 1
print(f"Computer chose scissor, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'paper' and comp_decision == 'rock':
hum_score += 1
print(f"Computer chose rock, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'scissor' and comp_decision == 'rock':
comp_score += 1
print(f"Computer chose rock, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'scissor' and comp_decision == 'paper':
hum_score += 1
print(f"Computer chose paper, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == comp_decision:
print("Computer chose the same, this round is a draw.")
else:
if hum_score == 3:
print ("Contragulations, you won!")
if comp_score == 3:
print("Sorry, you lost!")
It was working before, I forgot what exactly I changed, but am I missing something? Any help would be greatly appreciated, thanks!
I can’t say why it hangs, but the condition in your while loop doesn’t look right. hum_score is not being compared to 3 – if you fully parenthesize the logical expression, it would look like this:
while (hum_score) and (comp_score != 3):
...
As for the error, what is the error? Nobody can help without that kind of detail.
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
Thanks for the replies everyone, and P.S. I meant rock-paper-scissor game, not tic-tac-toe… not sure why I confused them
The error I was getting, for some reason, I’m unable to reproduce this morning… it said something about invalid syntax. But, I decided to go off on a limb and add parentheses to the boolean statement in the while loop, that seemed to do the trick. Still a bit confused… didn’t think those would be required, since they don’t seem to be required for the for-loops.
There’s nowhere you could insert parentheses that would make the logic of this consistently correct. If you parenthesize like this:
while (hum_score and comp_score) != 3:
It fails for these four results:
hum_score
comp_score
Expected
Actual
0
3
False
True
3
0
False
True
3
1
False
True
3
2
False
True
That’s because if you and two values in Python, you get the first one that is falsy or the last one if both are truthy. So 3 and 1 == 1, and 0 and 3 == 0, neither of which equal 3.
Instead, you could compare both values to 3 individually, or you could use the max function and compare the result to 3.
I think I got it working (it now has option to either play again, or quit).
from random import randint
while True:
hum_score = 0
comp_score = 0
while hum_score != 3 and comp_score != 3:
hum_decision = input("rock, paper, or scissor? ")
comp_options = ['rock', 'paper', 'scissor']
comp_decision = comp_options[randint(0,2)]
if hum_decision == 'rock' and comp_decision == 'paper':
comp_score += 1
print(f"Computer chose paper, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'rock' and comp_decision == 'scissor':
hum_score += 1
print(f"Computer chose scissor, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'paper' and comp_decision == 'scissor':
comp_score += 1
print(f"Computer chose scissor, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'paper' and comp_decision == 'rock':
hum_score += 1
print(f"Computer chose rock, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'scissor' and comp_decision == 'rock':
comp_score += 1
print(f"Computer chose rock, you lose this round! The score is {hum_score} to {comp_score}")
elif hum_decision == 'scissor' and comp_decision == 'paper':
hum_score += 1
print(f"Computer chose paper, you win this round! The score is {hum_score} to {comp_score}")
elif hum_decision == comp_decision:
print("Computer chose the same, this round is a draw.")
else:
if hum_score == 3:
print ("Contragulations, you won!")
if comp_score == 3:
print("Sorry, you lost!")
new_round = input("Do you want to play again? Press Y or N. ")
if new_round == "N":
print("Thanks for playing!")
break
I’m starting to think the original error message had to do with the compiler: now, when I paste the code into this online python compiler, it gives this error:
File "/home/main.py", line 12
print(f"Computer chose paper, you lose this round! The score is {hum_score} to {comp_score}")
^
SyntaxError: invalid syntax
But, when I paste it into THIS compiler, it works perfectly.