Python Forum

Full Version: TypeError: type str doesn't define __round__ method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am unsure how to use the round function for my problem, I am trying to write code that finds the cost of electricity using the wattage, hours and price, my code looks like this:

wattage = int(input("What is the wattage of your device?"))
hoursUsed = int(input("What is the amount of hours used?"))
pricePerKWh = 11.76
electricityCost = (wattage * hoursUsed)/ (1000 * pricePerKWh)
print("The cost of electricity is:" + round(str(electricityCost), 2))

If I don't use the round function the code works, however, the electricityCost ends up being a large decimal, which I do not want considering I am representing a dollar value, with the way my code is now I receive the message:

Traceback (most recent call last):
File "C:/Users/Windows/Documents/lab3.py", line 5, in <module>
print("The cost of electricity is:" + round(str(electricityCost), 2))
TypeError: type str doesn't define __round__ method

The answer may be obvious but I am new to Python, thanks in advance
There are different types of information. You can't round a string because it has no mathematical value. Convert to "Float" type if you want to round.

https://realpython.com/python-data-types/
I saw someone else say on here that you should never use floats for dollar values, is this true?

I changed to float and I receved the same message
(Feb-03-2020, 06:51 PM)emmapaw24 Wrote: [ -> ]I saw someone else say on here that you should never use floats for dollar values, is this true?

I changed to float and I receved the same message

You need to use floats for dollars. They use decimals and integers don't.

you still get this:
Error:
TypeError: type str doesn't define __round__ method
?

What code did you use?
I changed my code a bit and am now getting a different message:

wattage = float(input("What is the wattage of your device?"))
hoursUsed = float(input("What is the amount of hours used?"))
pricePerKWh = 11.76
electricityCost = (wattage * hoursUsed)/ (1000 * pricePerKWh)
print("The cost of electricity is:" + str(round(electricityCost), 2))
The error message I am getting now is:

What is the wattage of your device?100
What is the amount of hours used?720
Traceback (most recent call last):
  File "C:/Users/Windows/Documents/lab3.py", line 5, in <module>
    print("The cost of electricity is:" + str(round(electricityCost), 2))
TypeError: str() argument 2 must be str, not int
>>> 
I think it's your brackets. try this:
print("The cost of electricity is:" + str(round(electricityCost, 2)))
Yes it was!! Thank you so much! Smile
With f-string and in Python is better to us snake_case PEP-8.
wattage = float(input("What is the wattage of your device?"))
hours_used = float(input("What is the amount of hours used?"))
price_per_kWh = 11.76
electricity_cost = (wattage*hours_used) / (1000*price_per_kWh)
print(f"The cost of electricity is: {electricity_cost:.2f}")