Python Forum
TypeError: type str doesn't define __round__ method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: type str doesn't define __round__ method
#1
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
Reply
#2
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/
Reply
#3
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
Reply
#4
(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?
Reply
#5
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
>>> 
Reply
#6
I think it's your brackets. try this:
print("The cost of electricity is:" + str(round(electricityCost, 2)))
Reply
#7
Yes it was!! Thank you so much! Smile
Reply
#8
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}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to use type= as a function/method keyword argument Skaperen 9 1,777 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,208 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,099 May-07-2022, 08:40 AM
Last Post: ibreeden
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,660 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,967 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,331 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  TypeError: 'type' object is not subscriptable Stef 1 4,445 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,573 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,152 May-17-2020, 05:56 PM
Last Post: shockwave
  TypeError: ENCODE Method, str instead of byte Rajath 1 2,738 May-09-2020, 06:05 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020