Feb-03-2020, 05:59 PM
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
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