+ 1

Split func error

This code I wrote generates an error : words = input().lower().split(',') print(words) for x in words.split(): #error: “list “ object had no function split print(x) When this code generates no error and runs good: words, pattern = input().lower().split(',') words = [i.strip() for i in words.split()] # here is the same pattern = pattern.strip() print("Word '{}' appear {} time(s)".format(pattern, words.count(pattern)) I don’t understand that. When some can help me I would be cool. Ty guys

14th Mar 2018, 6:27 PM
Robert Kursatzky
Robert Kursatzky - avatar
4 Antworten
+ 9
firstly, it would be much more helpful if you made a code in playground so others can access it right away and see the issue firsthand (instead of pasting it here....) now for your issue: first, you take input, lowercase it and split it, and then assign it into words your issue arise when you attempt this line: for x in words.split(): words is a list at this point a list does not have a split function, therefore the error simply remove the ".split()" from that line and it should run
14th Mar 2018, 7:00 PM
Burey
Burey - avatar
+ 6
as i stated, in the mywords code, remove the .split() from the line for x in words.split(): as for the other code, you still got an issue there (it will only work for pairs of words seperated by "," ex. hello,world) https://code.sololearn.com/c2Bj3fNkyDFP/?ref=app https://code.sololearn.com/c3DgWYGwV1pY/?ref=app
14th Mar 2018, 7:20 PM
Burey
Burey - avatar
+ 1
I published this two files. You can look inside. Why I one example that works and in another not?
14th Mar 2018, 7:05 PM
Robert Kursatzky
Robert Kursatzky - avatar
0
Yes the “,” separates the inputs. When you make more “,” than once the program will be confused . But the expression: i.strip() for i in words.split() don’t cause the error and in the another code it does. I just don’t understand why . I have another one published as word1. Make no issue too. Besides there is no answer for all questions ;)
14th Mar 2018, 8:00 PM
Robert Kursatzky
Robert Kursatzky - avatar