+ 1

Here the programme supposed to sum only the odd numbers so what is the wrong in this code ? 🤔

numbers=(input()) list=[] for n in numbers: if n%2==0: list.append() else: continue print(sum(list))

11th Jan 2022, 4:25 PM
Mostafa Ahmad
Mostafa Ahmad - avatar
5 Respostas
+ 5
Mostafa Ahmad , your code is slightly reworked and has the respective comments: numbers = [int(i) for i in input().split()] # numbers has to be split and converted to int, input is done like: 1 2 3 4 5 6 lst=[] # please do not use variable names that are also used by python for n in numbers: if n%2==1: lst.append(n) # append was missing an argument inside parenthesis #else: # not required #continue # not required print(sum(lst))
11th Jan 2022, 6:36 PM
Lothar
Lothar - avatar
+ 3
If you really want the list just append it under sum += num n = int(input()) sum = 0 for num in range(n): if num % 2 == 0: sum += num print(sum)
12th Jan 2022, 5:04 AM
bakeery
+ 1
The remainder of dividing an odd number by 2 is 1
11th Jan 2022, 4:44 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
+ 1
1. numbers=int(input()) 2. I have no idea about task, but in for loop should be range of something, e.g. numbers or list of numbers. Or something like this: numbers= int(input()) sum=0 for _ in range(numbers): n = int(input()) if n%2: sum +=n print(sum)
11th Jan 2022, 5:31 PM
Alexey Kopyshev
Alexey Kopyshev - avatar
0
even it give an error ☹
11th Jan 2022, 5:17 PM
Mostafa Ahmad
Mostafa Ahmad - avatar