## Python : printing number sequences from nested loops

python nested for loop problem

while loop python

nested loops python exercises

python for loop multiple lines

for loop sequence python

for loop in for loop python

python range

I know it seems very basic and easy, and I'm sorry if I'm wasting everybody's time, but I'm stuck and it's been hours now.

I have 4 loops, and in the end I must print all the numbers from 1 to the end. It will be moe clear after you see the code:

for a in range (0,7): for b in range (1,118): for c in range (0,7): for d in range (1,118): print(...)

What I expect is :

1 2 3 ... 383292

where 383292=118*118*7*4

Keep in mind that I really need these 4 loops as they are shown.

What should I put inside the print? I feel so stupid right now because it seems to be a simple math problem but I can't seem to figure out the solution. Thanks a lot.

EDIT: to answers to all the comments, let's say I have this:

for a in range (0,2): for b in range (1,3): for c in range (0,2): for d in range (1,3): print(...)

I want to have as output:

1 2 3 ... 24

I need to find some operations to do on a,b,c and d to have my results. I hope it is a bit clearer.

(Yeah, the counter may be easier in most cases, but this is cooler!)

You need to know the amount of outputs that occur per iteration of each loop.

Assuming your loops have this many cycles:

cycles_a = 7 cycles_b = 117 cycles_c = 4 cycles_d = 117

You have outputs:

outputs_d = 1 outputs_c = outputs_d * cycles_d outputs_b = outputs_c * cycles_c outputs_a = outputs_b * cycles_b

If you run this loop:

for a in range(0,cycles_a): for b in range(0,cycles_b): for c in range(0,cycles_c): for d in range(0,cycles_d): print(d*outputs_d + c*outputs_c + b*outputs_b + a*outputs_a)

You will print:

0 1 ... <7*117*4*117 - 1>

If you want to start by printing 1, add one to the formula.

If your ranges start at 1 instead of 0, use `(a-1)`

/`(b-1)`

/`(c-1)`

/`(d-1)`

in the formula.

**python nested loop hailstone sequence for range,** Your program will print out the number that has the largest cycle length and what that cycle length is. Your sample session will look like this: Enter starting number � The for loop prints the number from 1 to 10 using the range () function here i is a temporary variable which is iterating over numbers from 1 to 10. It’s worth mentioning that similar to list indexing in range starts from 0 which means range ( j ) will print sequence till ( j-1) hence the output doesn’t include 6.

First, there is the straightforward and direct way:

counter = 1 for a in range (0,7): for b in range (1,118): for c in range (0,7): for d in range (1,118): print(counter) counter += 1

If we want to not do that, then we might note that it's as though you are writing a number in a mixed base (as opposed to base 10). It sort of seems like your number is in the form

A B C D | | | |__ base 118 | | |____ base 7 | |______ base 118 |________ base 7

To convert a number from this mixed base to base 10, you do

def convert(a, b, c, d): return d + 7 * c + 118 * 7 * b + 7 * 118 * 7 * a

(Or you reverse the digits and endian-ness).

So then you might do

for a in range (0,7): for b in range (118): for c in range (0,7): for d in range (118): print(d + 7 * c + 118 * 7 * b + 7 * 118 * 7 * a)

**1.13. Loops and Sequences — Hands-on Python Tutorial for Python 3,** There are two Python statement types to do that: the simpler for loops, which we take up def tripleAll(nums): ''' print triple each of the numbers in the list nums. I am new to python and I am having some problems with this code that should print all the prime numbers that are smaller than 50 using nested loops. Here is the code: i = 2 while(i < 50):

A simple solution (in the format you are doing it) would be:

counter = 0 myrange = lambda x: range(1,x+1) for a in myrange(118): for b in myrange(118): for c in myrange(7): for d in myrange(4): counter+= 1 print('counter %d gives: %d = %d * %d * %d * %d'%(counter,a*b*c*d,a,b,c,d))

`myrange`

runs a command starting at 1 and ending at x < maxnum+1 (ie. your max number).

The print uses cstyle formatting where `%d`

is an integer `%s`

is a string etc and subs in the arguments after `%`

.

This produces:

counter 1 gives: 1 = 1 * 1 * 1 * 1 ... counter 389869 gives: 97468 = 118 * 118 * 7 * 1 counter 389870 gives: 194936 = 118 * 118 * 7 * 2 counter 389871 gives: 292404 = 118 * 118 * 7 * 3 counter 389872 gives: 389872 = 118 * 118 * 7 * 4

Alternatively, you can save the values in a list and print them one at a time.

myrange = lambda x: range(1,x+1) output=[] for a in myrange(118): for b in myrange(118): for c in myrange(7): for d in myrange(4): output.append(a*b*c*d) for i in output: print(i)

This gives:

1 ... 250632 334176 97468 194936 292404 389872

If you need the values to be sored in size, you can do

output.sort() for i in output: print(i)

**Loops - Mathematical Python,** for n in [0,1,2,3,4,5]: square = n**2 print(n,'squared is',square) print('The for loop is For example, the sequence of squares from 1 to 100 can be constructed We can't use a list comprehension to build the list of Fibonacci numbers, and so� 1.13. Loops and Sequences¶. Modern computers can do millions or even billions of instructions a second. With the techniques discussed so far, it would be hard to get a program that would run by itself for more than a fraction of a second.

I guess that the easier way is:

res = 0 for a in range (0,2): for b in range (1,3): for c in range (0,2): for d in range (1,3): res += 1 print(res)

but it prints a max of 16 numbers, not 24.
You should start counting from 0 to n, or instead of `(1, 3)`

you should write `(1,4)`

if you want your cycle to repeat 3 times.

**Python 3 - Nested loops,** Python 3 - Nested loops - Python programming language allows the usage of one loop inside for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s) The print() function inner loop has end=' ' which appends a space instead of default newline. Hence, the numbers will appear in one row. Python Program to print Prime Numbers from 1 to 100 using For Loop. This python program display the prime numbers from 1 to 100. First, we used For Loop to iterate a loop between 1 and 100 values.

Another way to do it is using a generator:

>>> def infinite_counter(n=0): while True: n += 1 yield n >>> counter = infinite_counter() >>> for i in range(3): for j in range(2): print(next(counter)) 1 2 3 4 5 6

The `itertools.product`

is even better if you don't really need the ugly 4+ levels of nested `for`

loops (if you are discarding the values in the iterables and just want the counting):

>>> for i, _ in enumerate(product(range(3), range(2))): print(i+1) 1 2 3 4 5 6

**Python nested loops,** Python nested loops - Python programming language allows to use one loop for iterating_var in sequence: for iterating_var in sequence: statements(s) The following program uses a nested for loop to find the prime numbers from 2 to 100 − if not(i%j): break j = j + 1 if (j > i/j) : print i, " is prime" i = i + 1 print "Good bye!". Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. Syntax for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s) The syntax for a nested while loop statement in Python programming language is as follows −

**printing numbers with nested loops,** Python : printing number sequences from nested loops, (Yeah, the counter may be easier in most cases, but this is cooler!) You need to know the amount of� How to make this function general to create binary numbers? (many nested for loops) dospina: 4: 197: Jun-24-2020, 04:05 AM Last Post: deanhystad : Python beginner - nested while loops: mikebarden: 1: 132: Jun-01-2020, 01:04 PM Last Post: DPaul : best way out of nested loops? Skaperen: 7: 330: May-30-2020, 05:20 PM Last Post: Skaperen

**ProjPython – Nested loops,** This structure is called a nested loop, since one loop, the inner loop, prints each character within a string of length three even if the number has only one digit. Binary search was much faster than looking through every name in sequence. Loops in Python V22.0002-001 New Material Introduced in the one_minute_timer function • The range function –range takes one or two integers m and n as an arguments –when m is left out, it is (by default) set to 0 –creates a sequence of numbers from m to n • A for loop –The first line – for variable in sequence: • for and in are

**Python2 Tutorial: For Loops,** In each iteration step a loop variable is set to a value in a sequence or other continue print("Great, delicious " + food) # here can be the code for enjoying our range(n) generates an iterator to progress the integer numbers starting with 0� 100 90 80 70 60 50 40 30 20 10 When programming in Python, for loops often make use of the range() sequence type as its parameters for iteration. For Loops using Sequential Data Types

##### Comments

- Do you want to print the number just one time? or multiple times?
- Maybe start with a small range, and provide
`input`

and`output`

? - Not quite clear from your question but do you just need to declare
`e = 1`

at the start, print e, and then`e += 1`

after the print? - If I understood you correctly, you can put print statement below each
`for`

loop declaration, but then you'll have duplicated values. Another thing is the question, if you need these values to be ordered or not. Please provide clear input and output as @hansolo suggested. - Did you try a simple "counter" variable like David Buck suggested? Your math was wrong -- you will print 16 times because 16 == 2*2*2*2, which is the ranges you selected. You edited out the multiplication which shows your intent.
- That's exacctly what I wanted, Thanks a lot
- Now that's a clever way of getting around the ambiguities of the question. The counter won't end on the right number, but the second part of this answer will actually answer the original question, taking some liberties with the elipses to cover up problems in the original problem statement, and meet the "function of a,b,c,d" stipulation. You may have some math errors and start and end on the wrong numbers.