Python Forum
Python beginner - nested while loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python beginner - nested while loops
#1
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
Reply
#2
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why does Python not use parenthesis to contain loops? davidorlow 3 351 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  How to break out of nested loops pace 11 1,311 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 455 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 975 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Python for loops Kristenl2784 3 677 Jun-16-2020, 06:01 PM
Last Post: Yoriz
  python for loops waytohell007 3 829 Jun-03-2020, 06:03 AM
Last Post: Yoriz
  best way out of nested loops? Skaperen 7 1,307 May-30-2020, 05:20 PM
Last Post: Skaperen
  For loops in python Boinbo 3 805 Apr-18-2020, 01:23 AM
Last Post: buran
  For loops help, using python turtle SemiBeginnerPY 2 1,446 Mar-10-2020, 10:46 AM
Last Post: SemiBeginnerPY
  alternative to nested loops for large data set JonnyEnglish 2 862 Feb-19-2020, 11:26 PM
Last Post: JonnyEnglish

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020