Python Forum
Python beginner - nested while loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python beginner - nested while loops (/thread-27275.html)



Python beginner - nested while loops - mikebarden - Jun-01-2020

I have this code


#import the graphical interface
from tkinter import *
#creat root widget
x1 = 10
y1 = 10
x2 = 20
y2 = 20
root = Tk()

root.title('Evolution - Two Brancher')
root.geometry("600x600")
my_canvas = Canvas(root, width=300, height=300, bg="yellow")
#pady pushes canvas down the screen a little
my_canvas.pack(pady=30)
#create line (start x1, y1  end x2, y2, fill="colour")
Perform_branching_this_no_of_times = 0
generation_count = 1
while generation_count < 10:
#generation growth
	#inner loop
    while Perform_branching_this_no_of_times < 10:
	Perform_branching_this_no_of_times = Perform_branching_this_no_of_times + 1
		my_canvas.create_line(x1, y1, x2, y2, fill="black")
		x1 = x2
		y1 = y2
		x2 = x2 + 10
		y2 = y2 + 20
		my_canvas.create_line(x1, y1, x2, y2, fill="black")
		x2 = y2
		y2 = x2
		my_canvas.create_line(x1, y1, x2, y2, fill="black")
		generation_count = generation_count + 1

root.mainloop()
QUESTION\PROBLEM - I am very new to Python.Looking at code above , for generations 1 to 10, I wanted to perform the inner loop a different number for times, each time. To be exact

When generation count =1 , perform inner loop 2 times
When generation count = 2, perform inner loop 4 times
when generation count = 3, perform inner loop 8 times
when generation count = 4, perform inner loop 16 times

.....you see that the inner loop is always performed twice as many times as it was performed last time.

Anything to point me on my way to code the inner loop multiples would be very appreciated.
For an expert I think this will be a straightforward one.

THANKS
mike



RE: Python beginner - nested while loops - DPaul - Jun-01-2020

(You will be asked to put your code between tags, like so:)
inner = 2
for gencount in range(1,11):
    for innerloop in range(1,inner + 1):
        pass
    print('gencount {} innerloop {}'.format(gencount, innerloop))
    inner *= 2