0

I have this question that I've been trying to solve for the past hour but i've been getting it wrong, can someone help me?

Question:
Create a list of the odd numbers between 1 and n (include 1 as well as n -- if it's odd-- in the list). Associate the list with the variable odds .

My code:
odds = [1]
i = 0
for i <= n:
if i%2 != 0:
odds.append(i)
i+= 1

5
Contributors
7
Replies
11
Views
6 Years
Discussion Span
Last Post by pyTony
0

try this
gives you the idea. no checkups anyway

#Find odd numbers

def odd_find(fr, to):
    if fr > to:
        trans = fr
        fr = to
        to = trans
    odd = []
    noOdd = []
    
    for x in range(fr, to):
        if x % 2 == 0:
            odd.append(x)
        else:
            noOdd.append(x)

    print  'Odd Stuff', odd
    print 'Not odd', noOdd

odd_find(5,40 )

print out:
Odd Stuff [6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
Not odd [5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]

;)

0

We do not know context of your studies to know the purpose of this exersise. The purpose surely is not the correct output which can be directly produced with basic functions.

Edited by pyTony: n/a

0

Odd Stuff [6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
Not odd [5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39]

6 is even, 5 is odd, Your definition of even is odd :) And you are confusing the poor guy!

0
print 'Input n'
n = int(raw_input())
data = []
i = 1
while i <= n:
	data.append(i)
	i+=2

print data
0

We are not supposed to help to write the code but get this OP to learn from writing the code, anywhere here the code (without error handling):

print range(1,int(raw_input('Give the end: '))+1,2)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.