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)
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!'])
+ 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)
+ 1
Help
+ 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🙄
0
Your code??
where you are getting problem??
0
NEZ it separately prints the words like this:
['words = even!']
['In = even!']
0
Jaz
So where is your code??
Have you tried anything towards this problem??
we won't give direct answers here 🙄
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??
0
NEZ Yep
0
NEZ thanks for the big help
0
АлКа Релова i did a different code.
- 1
Hi