Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix this "TypeError: 'str' object is not callable" error?

I'm creating a basic program that will use a GUI to get a price of an item, then take 10% off of the price if the initial price is less than 10, or take 20% off of the price if the initial price is greater than ten:

import easygui
price=easygui.enterbox("What is the price of the item?")
if float(price) < 10:
    easygui.msgbox("Your new price is: $"(float(price) * 0.1))
elif float(price) > 10:
    easygui.msgbox("Your new price is: $"(float(price) * 0.2))

I keep getting this error though:

easygui.msgbox("Your new price is: $"(float(price) * 0.1))
TypeError: 'str' object is not callable`

Why am I getting this error?

like image 687
user2443381 Avatar asked Jun 01 '13 14:06

user2443381


People also ask

How do I fix TypeError str object is not callable?

The result was the TypeError: 'str' object is not callable error. This is happening because we are using a variable name that the compiler already recognizes as something different. To fix this, you can rename the variable to a something that isn't a predefined keyword in Python. Now the code works perfectly.

Why is my string object not callable in Python?

The Python "TypeError: 'str' object is not callable" occurs when we try to call a string as a function, e.g. by overriding the built-in str() function. To solve the error, make sure you're not overriding str and resolve any clashes between function and variable names.

How do I fix TypeError int object is not callable in Python?

But in Python, this would lead to the Typeerror: int object is not callable error. To fix this error, you need to let Python know you want to multiply the number outside the parentheses with the sum of the numbers inside the parentheses. Python allows you to specify any arithmetic sign before the opening parenthesis.

Are strings callable?

This is because strings are not functions. To call a function, you add () to the end of a function name.


1 Answers

You are trying to use the string as a function:

"Your new price is: $"(float(price) * 0.1)

Because there is nothing between the string literal and the (..) parenthesis, Python interprets that as an instruction to treat the string as a callable and invoke it with one argument:

>>> "Hello World!"(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Seems you forgot to concatenate (and call str()):

easygui.msgbox("Your new price is: $" + str(float(price) * 0.1))

The next line needs fixing as well:

easygui.msgbox("Your new price is: $" + str(float(price) * 0.2))

Alternatively, use string formatting with str.format():

easygui.msgbox("Your new price is: ${:.2f}".format(float(price) * 0.1))
easygui.msgbox("Your new price is: ${:.2f}".format(float(price) * 0.2))

where {:02.2f} will be replaced by your price calculation, formatting the floating point value as a value with 2 decimals.

like image 59
Martijn Pieters Avatar answered Sep 30 '22 22:09

Martijn Pieters