Reputation: 80
I've been trying to make a list with all common possibilities of the words in known_in_lines list . the problems is , however , because i'm using the "for loop" , every time "i" goes up one , the list will reset and therefor the code will end up with printing the last index of the list in lower case and stuff. is there a way i can make all of the indexes of the list for into the function and be returned all together ? basically , can i make insensitive_string list outside of the function so that it wont reset the list every time i use the function ?
def case_insensitive(*texts) :
insensitive_string = []
for text in texts :
insensitive_string.extend [text.lower(),text.upper(),text.capitalize()]
return (insensitive_string)
known_in_lines = ["hello" ,
"hi" ,
"what's up" ,
"how are you doing" ,
"how was your day" ]
for i in range (0,len(known_in_lines)) :
insensitive_string = case_insensitive(known_in_lines[i])
print (insensitive_string)
Upvotes: 3
Views: 54
Reputation: 149075
You could extend the list inside the loop, but in fact that is exactly what the case_insensitive
function does if you give it many arguments. Just do:
insensitive_string = case_insensitive(*known_in_lines)
You will get:
['hello', 'HELLO', 'Hello', 'hi', 'HI', 'Hi', "what's up", "WHAT'S UP", "What's up", 'how are you doing', 'HOW ARE YOU DOING', 'How are you doing', 'how was your day', 'HOW WAS YOUR DAY', 'How was your day']
Upvotes: 1
Reputation: 3961
There were some syntactical issues with the extend method (now fixed), and the printout has been nested into the loop together with an iteration counter for visibility:
def case_insensitive(*texts):
insensitive_string = []
for text in texts:
insensitive_string.extend([text.lower(),
text.upper(),
text.capitalize()
])
return insensitive_string
known_in_lines = ["hello",
"hi",
"what's up",
"how are you doing",
"how was your day"
]
for num, i in enumerate(range(len(known_in_lines))):
insensitive_string = case_insensitive(known_in_lines[i])
print(num)
print (insensitive_string)
print(case_insensitive(*known_in_lines))
Produces:
0
['hello', 'HELLO', 'Hello']
1
['hi', 'HI', 'Hi']
2
["what's up", "WHAT'S UP", "What's up"]
3
['how are you doing', 'HOW ARE YOU DOING', 'How are you doing']
4
['how was your day', 'HOW WAS YOUR DAY', 'How was your day']
['hello', 'HELLO', 'Hello', 'hi', 'HI', 'Hi', "what's up", "WHAT'S UP", "What's up", 'how are you doing', 'HOW ARE YOU DOING', 'How are you doing', 'how was your day', 'HOW WAS YOUR DAY', 'How was your day']
Upvotes: 1