Reputation: 463
So for this question, I have:
user_input = '5 1 7 8 2 1 3'
user_list = list(user_input.split())
even_nums = []
odd_nums = []
for i in range(len(user_list)):
if int(user_list[i]) % 2 == 0:
even_nums.append(user_list[i])
else:
odd_nums.append(user_list[i])
print(even_nums)
print(odd_nums)
even_sum = sum(even_nums)
odd_sum = sum(odd_nums)
if even_sum > odd_sum:
print('Even Wins!')
else:
print('Odd Wins!')
Upvotes: 1
Views: 177
Reputation: 679
user_input = '5 1 7 8 2 1 3'
user_list = list(map(int,user_input.split()))
even_nums = []
odd_nums = []
for i in range(len(user_list)):
if int(user_list[i]) % 2 == 0:
even_nums.append(user_list[i])
else:
odd_nums.append(user_list[i])
print(even_nums)
print(odd_nums)
even_sum = sum(even_nums)
odd_sum = sum(odd_nums)
if even_sum > odd_sum:
print('Even Wins!')
else:
print('Odd Wins!')
Upvotes: 0
Reputation: 5692
Overall your code is really, really close. You check for the even/odd by casting to an int, which is good. But you don't store the int in the new lists. You store the original string. If you add an extra step of pulling out the number and casting it as an int, your code will be more readable, and it will do what you want. For example:
user_input = '5 1 7 8 2 1 3'
user_list = list(user_input.split())
# ^^^^ don't need this. it's already a list
even_nums = []
odd_nums = []
for i in range(len(user_list)):
num = int(user_list[i]) # adding this one extra line makes the code easier to understand
if num % 2 == 0:
even_nums.append(num)
else:
odd_nums.append(num)
print(even_nums)
print(odd_nums)
even_sum = sum(even_nums)
odd_sum = sum(odd_nums)
if even_sum > odd_sum:
print('Even Wins!')
else:
print('Odd Wins!')
input() # waits for newline before letting the program close. :)
There are usually numerous ways to do something. And different people have different ideas as to what's the best way. But as long as you choose a way that works, that's what's most important when you're starting out.
If you're interested to see slightly tighter code, could also replace the first two lines of the for
loop with:
for n in user_list:
num = int(n)
Upvotes: 0
Reputation: 1
Here's my solution to convert string to int list
user_list = [int(number) for number in user_input.split(' ')]
Full Sourcecode
# string input
user_input = '5 1 7 8 2 1 3'
# convert string to int list
user_list = [int(number) for number in user_input.split(' ')]
even_nums = []
odd_nums = []
# assign values to even and odd list
for i in range(len(user_list)):
if int(user_list[i]) % 2 == 0:
even_nums.append(user_list[i])
else:
odd_nums.append(user_list[i])
print(even_nums)
print(odd_nums)
# calculate sum of even list
even_sum = sum(even_nums)
# calculate sum of odd list
odd_sum = sum(odd_nums)
# who win?
if even_sum > odd_sum:
print('Even Wins!')
else:
print('Odd Wins!')
Upvotes: 0
Reputation: 1857
You are creating a list of strings when you use .split()
instead do this :
user_list = [int(number) for number in user_input.split(' ')]
So the complete code is :
user_input = '5 1 7 8 2 1 3'
user_list = [int(number) for number in user_input.split(' ')]
even_nums = []
odd_nums = []
for i in range(len(user_list)):
if int(user_list[i]) % 2 == 0:
even_nums.append(user_list[i])
else:
odd_nums.append(user_list[i])
print(even_nums)
print(odd_nums)
even_sum = sum(even_nums)
odd_sum = sum(odd_nums)
if even_sum > odd_sum:
print('Even Wins!')
else:
print('Odd Wins!')
Upvotes: 3
Reputation: 201
You can use map function here.
Use
user_list = list(map(int, user_input.split()))
Instead of
user_list = list(user_input.split())
After change it, your code will work fine.
Upvotes: 1
Reputation: 924
You could just use list comprehensions
for converting str
in user_input to int
.
change
user_list = list(user_input.split())
to
user_list = [int(i) for i in user_input.split()]
Rest of your code should work fine
Upvotes: 0
Reputation: 11
change to
even_nums.append(int(user_list[i]))
else:
odd_nums.append(int(user_list[i]))
Upvotes: 0
Reputation: 59229
Your problem is you are creating a list of strings, instead of numbers. Change these lines
even_nums.append(user_list[i])
odd_nums.append(user_list[i])
to
even_nums.append(int(user_list[i]))
odd_nums.append(int(user_list[i]))
Upvotes: 1