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


Messages In This Thread
Python beginner - nested while loops - by mikebarden - Jun-01-2020, 12:42 PM
RE: Python beginner - nested while loops - by DPaul - Jun-01-2020, 01:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  reduce nested for-loops Phaze90 11 1,757 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Why both loops don't work in python? nau 3 1,055 Sep-21-2022, 02:17 PM
Last Post: rob101
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,636 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,173 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,238 Sep-18-2021, 12:59 PM
Last Post: muzikman
  Why does Python not use parenthesis to contain loops? davidorlow 3 3,399 Jun-09-2021, 06:33 AM
Last Post: Gribouillis
  How to break out of nested loops pace 11 5,262 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,272 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 4,329 Jun-24-2020, 04:05 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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