简体   繁体   中英

Having trouble finding the sum of a list

So for this question, I have:

  1. A string, which I converted to a list
  2. I found the odd and even numbers in that list and saved them into an odd_list and a even_list
  3. I want to compute the sum of each list, which should be an easy sum(list), but it keeps saying "int object is not callable" - but I'm not sure how to de-int a list.
  4. In case some of you may ask, the only reason I did int(user_list[i]) % 2 is because that's the only way I found to literate through my list to determine which element is even or odd - otherwise the system pops another error " not all arguments converted during string formatting
  5. Code below:
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!') 

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!') 

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]))

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.

change to

even_nums.append(int(user_list[i]))
else:
    odd_nums.append(int(user_list[i]))

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

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. Happy coding!

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)
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!') 

This will work as expected!

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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM