user9126689
user9126689

Reputation:

Python lists and ranges

I'm trying to practice my python so I can improve. I'm kinda stuck and not sure how to proceed. I get an error saying "can only concatenate list(not 'int') to list." I'll leave my code and what I'm trying to do below.

Upvotes: 1

Views: 2096

Answers (5)

user13609283
user13609283

Reputation: 1

word=input()
word_num=len(word)
print(word_num)

odd_num=[]
even_num=[]

for letters in range(0,word_num,2):
  odd_num.append(word[letters])

for letters in range(1,word_num,2):
  even_num.append(word[letters])

print(odd_num)
print(even_num)

This is the answer it works with every word, and follows all the requirements.

Upvotes: 0

Alkis Vounakis
Alkis Vounakis

Reputation: 56

I wrote this... Let me know what you think...

word = input("Choose a word to test: ")
word_len = len(word)
print(word," contains ",word_len," letters")
odd_letters = []
even_letters = []
for i in range(1,len(word),2):
    even_letters.append(word[i])
for i in range(0,word_len,2):
    odd_letters.append(word[i])

print("Odd letters are: ",odd_letters)
print("Even letters are: ",even_letters)

Upvotes: 1

Mahir Islam
Mahir Islam

Reputation: 1972

Your code is good, but i decided to find a quicker solution for the program you want. This is my code:

word = str(input("Enter word:"))
store_1 = [x for x in word]
store_2 = []
for idx, val in enumerate(store_1):
    store_2.append(idx)
even_numbers = [y for y in store_2 if y%2 == 0]
odd_numbers = [z for z in store_2 if z%2 == 1]
print("List of Even numbers:",even_numbers)
print("List of Odd numbers:",odd_numbers)

The variable 'word' takes in the word from the user. The list 'store_1' uses list comprehension to separate the letters the in the word and store it. Next, i enumerate through 'store_1' and use the variable 'store_2' to only store the indexes of 'store_1'.

Next, I declare another variable 'even_numbers' that uses list comprehension to iterate through 'store_2' and find the even numbers. The next variable 'odd_numbers' also uses list comprehension to find the odd numbers in 'store_2'.

Then, it just prints the even and odd lists to the user. Hope this helps :)

Upvotes: 1

Amal
Amal

Reputation: 135

Few things: You cannot "add" an integer directly to a list using '+'. Using append() would be best. str and str types can be concatenated using '+' so you could change odd_letters and even_letters to str as shown below.

also, by adding 'i' to even and odd, you are adding the iteration variable value. Since you want the letter to be appended, you need to refer the list index i.e word[i]

and the first letter of what is entered will be at an odd position :)

word = input("Type: ")

word = list(word)
print(word)

odd_letters = ''
even_letters = ''

length = int(len(word))

for i in range(1,length+1):
    if i%2 == 0:
        even_letters = even_letters + word[i-1]
    else:
        odd_letters = odd_letters + word[i-1]

print("even_letters",even_letters)
print("odd_letters",odd_letters)

Upvotes: 0

sjw
sjw

Reputation: 6543

You cannot add an integer to a list, as you have attempted to do here:

even_letters = even_letters + i

You can instead do this (which is now adding a list to a list, which is valid):

even_letters = even_letters + [i]

Or, use append to alter the list in-place, adding the new element to the end:

even_letters.append(i)

Upvotes: 0

Related Questions