I'm really new to coding and Python and have beent trying the "rock, paper, scissor" game. Everything seems to work except the looping and i'm really struggling to understand why, I thought that setting the player to False would reloop the code?
from random import randint
t = ["rock", "paper", "scissors"]
computer = t[randint(0, 2)]
player = False
while player == False:
player = input("rock, paper or scissors?")
if player == computer:
print("Tie!")
elif player == "rock":
if computer == "paper":
print ("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "paper":
if computer == "scissors":
print("sorry, you lose", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "scissors":
if computer == "rock":
print("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destorys", computer)
else:
print("not a valid input, try again")
player = False
computer = t[randint(0, 2)]
Any help is welcome!
CodePudding user response:
You are overwriting player
with an string in the line with input()
. Then player
is never equal to False
. If you want to loop endless you can simply do it with a while True:
. Then it is even better readable.
CodePudding user response:
python creates code blocks based on indentation.
Look at the indentation of your second player = False statement, that should help :)
CodePudding user response:
Indent these lines so they are in the while
loop
player = False
computer = t[randint(0, 2)]
CodePudding user response:
Your Player variable is a boolean and a string The string is "rock", "paper" or "scissors" so it is bool(Player) = True. You need 2 variables, game_loop (bool) and player_response (string)
The computer choice have to be in the loop (computer = t[randint(0, 2)])
CodePudding user response:
In Python, strings (text) are True as long as they are not empty. Similarly, lists, sets and other collections are True unless empty. It has a great advantage once you are aware of it. Also, your indentation was wrong. You need to move the computer=t[randint(0,2)] into the loop to pick a new choice each time. Setting player=False at the end does not make sense.
from random import randint
t = ["rock", "paper", "scissors"]
player = None
while player != "quit":
computer = t[randint(0, 2)]
player = input("rock, paper or scissors?")
if player == computer:
print("Tie!")
elif player == "rock":
if computer == "paper":
print ("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "paper":
if computer == "scissors":
print("sorry, you lose", computer, "beats", player)
else:
print("Great, you win!", player, "destroys", computer)
elif player == "scissors":
if computer == "rock":
print("sorry, you lose!", computer, "beats", player)
else:
print("Great, you win!", player, "destorys", computer)
elif player != "quit":
print("not a valid input, try again")