Im trying to get the user to input the length and width of a rectangle at the same time.
length,width = float (raw_input("What is the length and width? ")).split(',')
When I run the program, however, and enter two variables such as 3,5 I get an error saying that I have an invalid literal for type float().
First, why does this fail:
The
split(',')splits a string into a sequence of strings. You can’t callfloaton a sequence of strings, only on a single string. That’s why the error says it’s “an invalid literal for type float”.If you want to call the same function on every value in a sequence, there are two ways to do it:
Use a list comprehension (or a generator expression):
Or the
mapfunction:I would use the list comprehension, because that’s what the BDFL prefers, and because it’s simpler for other things you may want to do like
x[2], but it really doesn’t matter that much in this case; it’s simple enough either way, and you should learn what both of them mean.