1

So, I tried running this code in both Jupyter and Spyder. It gave me the correct output in the Jupyter notebook but showed be TypeError: 'str' object is not callable in Spyder. Please explain why is it happening. Also, I tried writing float(guess) in the call function but still didn't work.

x = 25
epsilon = 0.01
step = 0.1
guess = 0.0

while abs(guess**2-x) >= epsilon:
if guess <= x:
    guess += step
else:
    break

if abs(guess**2 - x) >= epsilon:
    print('failed')
else:
    print('succeeded: ' + str(guess))
ashirwad
  • 11
  • 3
  • What is the _full_ error traceback? – DavidG Oct 02 '18 at 14:29
  • 3
    Possible duplicate of [Builtin function not working with Spyder](https://stackoverflow.com/questions/47738537/builtin-function-not-working-with-spyder) – roganjosh Oct 02 '18 at 14:31
  • ^ Tentative dupe, but the amount of times it ends up being the root cause, I'm posting anyway – roganjosh Oct 02 '18 at 14:32
  • @roganjosh I agree, was just about to post a comment saying either `abs` or `str` has been renamed – DavidG Oct 02 '18 at 14:33
  • @DavidG it's particularly bad in Spyder over pretty much every other IDE because they followed a MATLAB approach to the namespace. And in the case that it is the cause, it usually ends with a big discussion in comments and OP being hammered with downvotes because it's very unintuitive, so I'm shooting from the hip :) – roganjosh Oct 02 '18 at 14:34
  • (*Spyder maintainer here*) What do you mean by *MATLAB approach to the namespace*? If you redefine `str` in Jupyter, it'll happen the exact same error. – Carlos Cordoba Oct 02 '18 at 14:53
  • @CarlosCordoba In our previous discussion, you said that the namespace model _defaults_ to this behaviour because MATLAB users [are very accustomed to it](https://stackoverflow.com/questions/47738537/builtin-function-not-working-with-spyder#comment82442313_47738834). It's the reverse of what I would anticipate to be the case and I have seen plenty of questions with this issue coming up, but not with, say, Canopy or other IDEs. It's also why, I think, the questions tend to get badly received because the people trying to help are also not expecting this to be a root cause – roganjosh Oct 02 '18 at 14:56
  • 1
    @roganjosh, I forgot about my own comment, sorry. I'll discuss with my team if resetting the namespace after each evaluation is a better approach to avoid this kind of issues in the future. Or at least, evaluating your code in an empty namespace, instead of the one present in the console. – Carlos Cordoba Oct 02 '18 at 14:59
  • @CarlosCordoba it is what it is, though, it's just something to get used to. I really just wanted to ensure that the OP didn't get blasted like usual; note that the dupe was on -2 before I answered and it was a lengthy discussion in comments. Perhaps it's a simple case of a configuration popup when you first start to give the user a 1-off choice based on their background, which can be reconfigured later on. At least to keep both sides happy – roganjosh Oct 02 '18 at 15:00
  • I did a little reformatting and ir ran on both spyder and vscode. – Natsfan Oct 03 '18 at 23:00

0 Answers0