I have a problem with Python. This is my code:
def calcola():
a = input()
b = float(a[0].split("*"))
c = float(a[0].split("/"))
d = float(a[0].split("-"))
e = float(a[0].split("+"))
j = float(a[1].split("*"))
k = float(a[1].split("/"))
l = float(a[1].split("-"))
m = float(a[1].split("+"))
b = b[0]
c = b[1]
d = c[0]
e = c[1]
f = d[0]
g = d[1]
h = e[0]
i = e[1]
somma1 = b+c
somma2 = d+e
somma3 = f+g
somma4 = h+i
print(somma1)
print(somma2)
print(somma3)
print(somma4)
calcola()
I've recieved some errors:
Traceback (most recent call last): File "file.py", line 29, in calcola() File "file.py", line 3, in calcola b = float(a[0].split("*")) TypeError: float() argument must be a string or a number, not 'list'
How can I transform the number in the list?
You can't call float
on a list directly. You can use map
to call float
on each item in the list. Like so:
b = map(float, a[0].split("*"))
In python 3.x
b = list(map(float, a[0].split("*")))
Or for more readability, use a list comprehension. Works for both python2 and python3:
b = [float(s) for s in a[0].split("*")]
But be sure the items after splitting are floatable
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments