I have no clue how to start this
Use the for and while iterative structures within Python to program a loop that generates a list of odd numbers from 1 to 33 inclusive. (two sets of code here!)

Recommended Answers

All 6 Replies

You need to show some effort! We won't write out the code for you. Research for and while loops on the official python documentation. Make a variable that will be the condition for the loop. Increment this by two each time, making sure it starts on one, and stops when it reaches 33.

import random
for i in range (5):
# random integer: 1 <= number <= 33
print random.randint(1, 34)
I got this for the for statement but i cant get it to exclude evens, and loop

import random
while 1:
print random.randrange (0,34)
and I got this for the while but I cant get it to exclude evens

Why random number?

Can you say odd numbers from smallest to biggest until 33 inclusive? How could computer do the same?

Here's a hint, look at the modulo operator (%) and utilize conditional statements (if).

That would be a good idea Convoluted, but it may overcomplicate things. You just need a simple loop where you have a variable, say "i" (starting at 1), then each loop iteration, you write the value of "i" to an array each each loop, the position of which increases by 1.

Try and have a good around in the other threads. This has been done myriad times.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.