-1

Afternoon everyone, I'm having some problems trying to seperate an inputted word within a string into odd and even categories. The problem is supposed to return two output values with the odd and even letters allocated to those respective branches.

Now I have run some trials in which the string was hard coded into the wordStr, and the rest of the process seems to run smoothly under that. So i'm feeling that I'd have to appended something different in order to the program to register the user input. Here's what the code looks like:

def oddEvenWord():

    #Objective: program will print out the seperate characters included within a string under where they fall placement wise, odd or even

    wordstr = eval(input("Please enter a word to be broken apart into odd and even characters:"))

    even_letters = ""
    odd_letters = ""
    lword = len(word)
    index = 0


    for index in range(0,len(word)):
        if int(index) % 2 == 0:
            even_letters += word[index]

        else:
            odd_letters += word[index]

    print(list(even_letters), end = ' ')
    print(list(odd_letters), end = ' ')

oddEvenWord()

Like I mentioned above, the program should be able to distinguish between and seperate the odd characters from the even characters in a user inputted word, but the module fails to recognize it as a valid string, as well as invalidating the callback. I'd appreciate it if anyone could give me some pointers in this department

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55

2 Answers2

0

By calling eval you're treating the input string as Python code, which it is not, so don't call eval. You should also name the receiving variable word instead of wordstr to be in line with the rest of your code:

Change:

wordstr = eval(input("Please enter a word to be broken apart into odd and even characters:"))

to:

word = input("Please enter a word to be broken apart into odd and even characters:")
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

To get multiple words with spaces, you can use raw_input()

As below,

word = raw_input("Please enter a word to be broken apart into odd and even characters:")

Otherwise be aware while typing your input string that don't forget to type "" And as @blhsing mentioned in his answer, you should remove eval and replace wordstr with word

And even you can do the operation in python one liner like follows:

Either of a one liner

a=lambda x:[[x[o] for o in range(len(x)-1) if not o%2],[x[e] for e in range(len(x)-1) if e%2 ]]

or

a=lambda x:[[o for o in x[::2]],[e for e in x[1::2]  ]]

then

word = raw_input("Please enter a word to be broken apart into odd and even characters:")
print(a(word))
Vanjith
  • 520
  • 4
  • 23