0

I have tried to add a 'while True' loop but it shows the error 'str object is not callable'. Please help me on this one. I need an extension of this already written code game.

import random
user_score = 0
computer_score = 0
user_score = 0
computer_score = 0
input = input("Choose your move, Rock, Paper or Scissors: ").upper()
comp = ["ROCK", "PAPER", "SCISSORS"]
computer_move = random.choice(comp)

if input == "ROCK":
    if computer_move == "PAPER":
        print("You lost. Better luck next time!")
        computer_score += 1
    elif computer_move == "SCISSORS":
        print("You won! Well done.")
        user_score += 1
elif input == "PAPER":
    if computer_move == "ROCK":
        print("You won! Well done.")
        user_score += 1
    elif computer_move == "SCISSORS":
        print("You lost. Better luck next time!")
        computer_score += 1
elif input == "SCISSORS":
    if computer_move == "ROCK":
        print("You lost. Better luck next time!")
        computer_score += 1
    elif computer_move == "PAPER":
        print("You won! Well done.")
        user_score += 1
elif input == computer_move:
    print("It's a tie!")
print(f"Your Score: {user_score} ; Computer Score: {computer_score} ")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Which line do you get the error? Share the complete traceback. – Jarvis Dec 29 '20 at 10:39
  • I've removed the [tag:pygame] tag. The [tag:pygame] tag addresses the [PyGame](https://www.pygame.org/news) library, but is not intended for a game written in Python. – Rabbid76 Dec 29 '20 at 10:43

3 Answers3

1

This is happening because you are defining your input variable as this:

input = input("Choose your move, Rock, Paper or Scissors: ").upper()

input is a built-in function in python, don't use variables with keywords/built-in functions as names. In your second iteration inside the while, when you try to take input with the input() function, it gives an error since you overshadowed it with your string input (Python is dynamically-typed) so it's no longer the function <built-in function input> which could help you take the input in current scope. You can define it like this:

choice = input("Choose your move, Rock, Paper or Scissors: ").upper()
Jarvis
  • 8,494
  • 3
  • 27
  • 58
0

You can try something like this:

while(True):
  # your code
  answer = input("Do you want to play again? (y / n): ")
  if (answer == "n") break

With this code, the player will be asked to input either "y" or "n". If the answer is anything different than "n", the game will re-start. Otherwise it will not (the program will break out of the loop).

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31
0

the problem you encountered resulted from naming the variable holding the user input input thus when using the function input the secon time it instead tried calling the variable holding the string of the previous use of input.

change the name of the variable input to something different like user_input and then looping should work.

ereldebel
  • 165
  • 11