0

Go through the string below and if the length of a word is even print "even!" str = 'Print every word in this sentence that

Expected Ouput: [ 'word = even!', 'in = even!', ... ] My code: def even(str): str = str.split() for a in str: if len(a)%2 == 0: a += 'even!' print (a)

13th Feb 2022, 4:48 AM
Jaz
Jaz - avatar
14 Réponses
+ 4
Jaz I didn't understood your code, what you are doing there :| So, here's the code: txt = "Print every word in this sentence that" x = txt.split() print(x) print() for word in x: if len(word)%2==0: print([word,'even!']) else: print([word, 'odd!'])
13th Feb 2022, 5:13 AM
NEZ
NEZ - avatar
+ 2
Jaz I would like to propose a different method using a def as per your original concept. The def assesses the length of the word and prints the output if it is even. I created an external loop and iterated the function through the items of the list str = 'Print every word in this sentence that' def even(txt): if len(txt)%2 == 0: print(txt," -> even") for i in str.split(" "): even(i)
13th Feb 2022, 9:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Help
14th Feb 2022, 1:27 PM
Gul Yousuf
Gul Yousuf - avatar
+ 1
Hi АлКа Релова, we need to input a sentence (say: Hello world) Now the program will calculate every word whether it's odd or even (not the whole sentence). Like: Hello: odd World: odd edit]: you can share your program too🙄
15th Feb 2022, 4:05 AM
NEZ
NEZ - avatar
0
Your code?? where you are getting problem??
13th Feb 2022, 4:50 AM
NEZ
NEZ - avatar
0
NEZ it separately prints the words like this: ['words = even!'] ['In = even!']
13th Feb 2022, 4:52 AM
Jaz
Jaz - avatar
0
Jaz So where is your code?? Have you tried anything towards this problem?? we won't give direct answers here 🙄
13th Feb 2022, 4:56 AM
NEZ
NEZ - avatar
0
Jaz ['Print', 'every', 'word', 'in', 'this', 'sentence', 'that'] ['Print', 'odd!'] ['every', 'odd!'] ['word', 'even!'] ['in', 'even!'] ['this', 'even!'] ['sentence', 'even!'] ['that', 'even!'] [Program finished] // Is this output you want to print??
13th Feb 2022, 5:08 AM
NEZ
NEZ - avatar
0
NEZ Yep
13th Feb 2022, 5:09 AM
Jaz
Jaz - avatar
0
NEZ thanks for the big help
13th Feb 2022, 5:14 AM
Jaz
Jaz - avatar
0
АлКа Релова i did a different code.
15th Feb 2022, 8:55 AM
Jaz
Jaz - avatar
- 1
Hi
14th Feb 2022, 3:58 PM
Aminur Rhman