1

I'm trying to add the results of one list to another list, but I keep getting this error. What's going on?

I've tried to write the resistance * current equation in every order possible. I'm required to include a 'for' loop, input statements (NOT eval(input)), and it only involves entering in five variables for the current statement.

current = []
power = []
current = input("Enter in your numbers: ")
current = current.split()
current = [eval(x) for x in current]
resistance = [16, 27, 39, 56, 81]
print("[resistance, current, power]")
power = current * resistance
for i in range(5):
     print([resistance[i], current[i]], power[i])
print([sum(resistance), sum(current)])

I keep getting the error: TypeError: can't multiply sequence by non-int of type 'list'

I want to be able to multiply current^2 with resistance, and store that in a variable called 'power.'

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Jake M
  • 41
  • 5
  • Could you give an input example? – Pau Apr 04 '19 at 20:07
  • 4
    `'12345'.split()` returns `['12345']`, just as a heads up – C.Nivs Apr 04 '19 at 20:07
  • If I were you I would make those two lists fixed in the script and of the same length, because from what I see you want to multiply one value from the currents list with one from resistances list, and if you put too few or too many values in the input you will end up with weird results. – andreihondrari Apr 04 '19 at 20:12
  • 1
    instead of trying to multiply the lists as whole, do it element-wise. Or use something like numpy. – Nipun Thennakoon Apr 04 '19 at 20:14
  • 1
    Don't use `eval`; if the current is supposed to be a float, use `float`. `eval` opens you up to arbitrary code evaluation. – chepner Apr 04 '19 at 20:21

0 Answers0