0

I am getting an error in my program, can you help please?

This is the relevant code:

    def play():
        play = input("Do you want to play again? (y/n) :")
        if play == "y":
            game(0)
        if play == "n":
            quit()
        if play == "Debug":
            game(1)
        if play != "y" or "n" or "Debug":
            play()

This is the error:

    Well Done You Win!!!!!
    You took this many tries : 
    1
    Game Was Made By Ross Searle!!!
    Do you want to play again? (y/n) :gnl;rgn;
    Traceback (most recent call last):
      File "C:\Users\ross\Desktop\game.py", line 67, in <module>
        firstplay()
      File "C:\Users\ross\Desktop\game.py", line 7, in firstplay
        game(0)
      File "C:\Users\ross\Desktop\game.py", line 55, in game
        play()
      File "C:\Users\ross\Desktop\game.py", line 23, in play
        play()
    TypeError: 'str' object is not callable
    >>>    

I am trying to make it so it just repeats the question if you type anything.

timgeb
  • 76,762
  • 20
  • 123
  • 145

2 Answers2

3

The problem here is that you are having a function called play, and in that function you assign the same name to a string play. Now play() will try to call a string object, which is obviously going to fail. The error-message is actually telling you this:

TypeError: 'str' object is not callable

The solution is to give the user input another name, for example:

play_input = input("Do you want to play a Game? (y/n) :")

I also fixed the typo in the prompt for you ;)

timgeb
  • 76,762
  • 20
  • 123
  • 145
3
play = input("Do ypu want to play a Game? (y/n) :")

This defines play as a string variable.

You also play as a function with:

def play():

Python then doesn't know what to do when you attempt to call the function play when it's really dealing with a string called play. Python just thinks you are trying to use the string as a function and because this is not possible it then gives you the error message TypeError: 'str' object is not callable

To fix this change your names so that no variable names are used as function names and vice-versa.

shuttle87
  • 15,466
  • 11
  • 77
  • 106