0

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!

Albie
  • 3
  • 2
  • your final ```player = False``` needs to be indented such that it is in the ```while``` loop – Nin17 Jul 20 '22 at 09:40
  • When the player ande the computer both have the same, your code gives the win to the player. Shouldn't that be a draw? – alanturing Jul 20 '22 at 09:47
  • Besides the obvious indenting-based typo: consider to simply NOT "overload" the player variable like this. Controlling your loop is one thing, asking for user input to advance your game is a different one. So why not use TWO different variables for example? – GhostCat Jul 20 '22 at 10:09
  • 1
    Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – GhostCat Jul 20 '22 at 10:10

5 Answers5

1

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.

alanturing
  • 214
  • 1
  • 8
0

python creates code blocks based on indentation.

Look at the indentation of your second player = False statement, that should help :)

skpn
  • 89
  • 1
  • 5
0

Indent these lines so they are in the while loop

player = False
computer = t[randint(0, 2)]
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
0
  • 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)])

djangoliv
  • 1,698
  • 14
  • 26
0

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