0

I am trying to learn Python. I am currently writing my first program and following Automate the Boring Stuff. I have run into an error that I cannot understand. I am running python 3.9.1. The lines in question run perfectly fine on their own, but give me the following error:

line 11, in <module>
    print ('Pick a number.')
TypeError: 'str' object is not callable

Here is the total code:

import random

print ('Hello! What is your name?')
name = input()

print = ('Hi, ' + name + '. I am thinking of a number between -99 and 99.')
secretNumber = random.randint(-99,99)

for guessesTaken in range(1,7):
    print ('Pick a number.')
    guess = int(input())
    
    if guess < secretNumber:
        print ('Your guess is too low. Guess again.')
    elif guess > secretNumber:
        print('Your guess is too high. Guess again.')
    else:
        break # This means the guess was correct

if guess == secretNumber:
    print ('Good job, ' + name + '. You guessed the value in ' + str(guessesTaken) + ' guesses.')
else:
    print ('You did not guess the number. The correct number was ' + str(secretNumber) + '.')
MarianD
  • 13,096
  • 12
  • 42
  • 54
new2coding
  • 13
  • 1
  • 4
    You accidentally set `print` to a string in the 6th line instead of printing it. Remove the `=` which is creating a variable with the same name as the built-in `print`. Please do not use reserved names to name your variables or these error messages will be frequent and rather confusing :) – gmdev Dec 26 '20 at 19:29

2 Answers2

1

The problem is in the line 6 actually, where you try to assign an value to the print:

print = ('Hi, ' + name + '. I am thinking of a number between -99 and 99.')

You should delete that = and the code will work :)

import random

print('Hello! What is your name?')
name = input()

print('Hi, ' + name + '. I am thinking of a number between -99 and 99.')
secretNumber = random.randint(-99,99)

for guessesTaken in range(1,7):
    print ('Pick a number.')
    guess = int(input())
    
    if guess < secretNumber:
        print ('Your guess is too low. Guess again.')
    elif guess > secretNumber:
        print('Your guess is too high. Guess again.')
    else:
        break # This means the guess was correct

if guess == secretNumber:
    print ('Good job, ' + name + '. You guessed the value in ' + str(guessesTaken) + ' guesses.')
else:
    print ('You did not guess the number. The correct number was ' + str(secretNumber) + '.')
Matheus Delazeri
  • 425
  • 2
  • 15
0

Because of your line

print = ('Hi, ' + name + '. I am thinking of a number between -99 and 99.')

where your redefined the built-in function print to be a string.

Remove = from that command:

print('Hi, ' + name + '. I am thinking of a number between -99 and 99.')
MarianD
  • 13,096
  • 12
  • 42
  • 54