+ 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))
5 Respuestas
+ 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))
+ 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)
+ 1
The remainder of dividing an odd number by 2 is 1
+ 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)
0
even it give an error ☹