Python Forum
TypeError: can't multiply sequence by non-int of type 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: can't multiply sequence by non-int of type 'str'
#1
Hi, I'm new to python so the answer to this may be obvious, but I'm trying to write code to find the cost of electricity used by a device using wattage, the number of hours used, and electricity cost. The code I've written is:

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

When I run this is recieve:

What is the wattage of your device?3
What is the amount of hours used?3
Traceback (most recent call last):
File "C:/Users/Windows/Documents/lab3.py", line 4, in <module>
electricityCost = ((wattage * hoursUsed)/(1000 * pricePerKWh))
TypeError: can't multiply sequence by non-int of type 'str'
>>>

I can't figure out what I'm doing wrong in the code, again this might be obvious but I'm new to this, thanks in advance.
Reply
#2
The input function returns a string, so you need to convert those values to numeric ones, using the int or float functions as appropriate.
Reply
#3
There are different "types" of information in python. "int" refers to "integer", and "str" refers to "string". You inputs collect a "string", which is just a series of characters, could be letters or numbers and has no mathematical value. If you want to do math with a string you need to convert it to a number-type variable first.

#here is one way
electricityCost = ((wattage * int(hoursUsed))/(1000 * int(pricePerKWh)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 9,207 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 2,096 May-07-2022, 08:40 AM
Last Post: ibreeden
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,024 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  Why can't I explicitly call __bool__() on sequence type? quazirfan 11 4,539 Aug-20-2021, 06:49 AM
Last Post: Gribouillis
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 2,965 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,023 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 7,323 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  TypeError: 'type' object is not subscriptable Stef 1 4,440 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: unhashable type: 'set' Stager 1 2,571 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  TypeError: __repr__ returned non-string (type dict) shockwave 0 3,150 May-17-2020, 05:56 PM
Last Post: shockwave

Forum Jump:

User Panel Messages

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