testdata = ["One,For,The,Money", "Two,For,The,Show", "Three,To,Get,Ready", "Now,Go,Cat,Go"]
#My Code:
def chop(string):
x = 0
y = 0
while x < 5:
y = string.find(",") 1
z = string.find(",", y)
x = x 1
return y, z
#My Code Ends
for i in range(4):
uno, dos, tres, cuatro = chop(testdata[i])
print(uno ":" dos ":" tres ":" cuatro)
It say I don't have enough values, I previously tried appending similar code to a list and it said I had too many
CodePudding user response:
I cant figure out why You need to do in that way, but maybe it helps.
testdata = ["One,For,The,Money", "Two,For,The,Show",
"Three,To,Get,Ready", "Now,Go,Cat,Go"]
for i in testdata:
uno, dos, tres, cuatro = i.split(',')
print(uno ":" dos ":" tres ":" cuatro)
Result
One:For:The:Money
Two:For:The:Show
Three:To:Get:Ready
Now:Go:Cat:Go
Just iterate through array and spllit words by ,
. Result is as expected.
CodePudding user response:
You can search for the position of the ,
(comma) in the given line and apply sweeping technique to insert the words into a list. You were getting too few values as the function was not returning 4 elements that is being extracted in the for loop.
testdata = ["One,For,The,Money", "Two,For,The,Show", "Three,To,Get,Ready",
"Now,Go,Cat,Go"]
# My Code:
def chop(line):
start_position = 0
words = []
for i, c in enumerate(line):
if c == ",":
words.append(line[start_position:i])
start_position = i 1
words.append(line[start_position:])
return words
# My Code Ends
for i in range(4):
uno, dos, tres, cuatro = chop(testdata[i])
print(uno ":" dos ":" tres ":" cuatro)
Output:
One:For:The:Money
Two:For:The:Show
Three:To:Get:Ready
Now:Go:Cat:Go
Explanation:
Here we are keeping the start position of each word in the start_position
variable.