“Python for Loop” by Erik Winge
https://openprocessing.org/sketch/906164
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Press “spacebar” or click mouse to stop/start the animation. Press any key to advance one step.
A fork of Visualization of a while Loop by Diana Lange (Teachings)
CC Attribution ShareAlike
Python for Loop
Winge
xxxxxxxxxx
PFont font;
int fontSize = 16;
String [] program = {"sentence = ['a', 'short', 'list']",
"print('Iterating over the sentence:')",
"for word in sentence:",
" # loop starts (indentation)",
" print(word)",
" # loop/indentation ends",
"print('Done')"};
String[] list = {"a", "short", "list"};
ArrayList<String> output= new ArrayList<String>();
int activeID = 0;
boolean doLoop = true;
int delayFrame = 60;
float lineSpacing = fontSize*1.3;
String word = "";
int count;
void setup() {
size(500, 500);
smooth();
font = createFont("Courier", fontSize);
frameRate(30);
textFont(font, fontSize);
textAlign(LEFT);
}
void draw() {
background(255);
fill(0);
noFill();
stroke(0);
drawText();
drawFigure();
if (doLoop) autoupdate();
}
void drawText() {
pushMatrix();
//left+top margin
translate(15, fontSize*2);
for (int i = 0; i < program.length; i++) {
//highlight active program line
if (i == activeID) {
rect(0, 5, width-30, -fontSize-5);
}
printText(program[i]);
}
printText("");
printText("Output:");
for (String line: output) {
printText(line);
}
popMatrix();
}
void textbox(String content, String label,int x,int y){
int boxWidth = 120;
int boxHeight = 30;
x = x-boxWidth/2;
rect(x, y+2, boxWidth, boxHeight);
text(content, x+10, y+lineSpacing);
text(label, x, y-5);
}
private void drawFigure() {
int top = height/2;
int bottom = height;
int distance = (bottom - top)/3;
int listPos = top + distance;
int wordPos = bottom - distance;
textbox("'a'", "sentence[0]", width/4, listPos);
textbox("'short'", "sentence[1]", width/2, listPos);
textbox("'list'", "sentence[2]", width-width/4, listPos);
textbox("".equals(word) ? "" : "'"+word+"'", "word", width/2, wordPos);
switch (count) {
case 1:
arrow(width/4+40, listPos+40, width/2, wordPos-10);
break;
case 2:
arrow(width/2, listPos+40, width/2, wordPos-10);
break;
case 3:
arrow(width-width/4-40, listPos+40, width/2, wordPos-10);
break;
}
}
private void printText(String content) {
text(content, 20, 0);
translate(0, lineSpacing);
}
void autoupdate() {
if ((frameCount % delayFrame) == 0) {
update();
}
}
void update() {
activeID++;
if (activeID >= program.length -1 && count < list.length) {
activeID = 3;
}
if (activeID == 1) {
output.add("Iterating over the sentence:");
}
if (activeID == 3) {
word = list[count];
count++;
}
if (activeID == 4) {
output.add(word);
}
if (activeID == 6) {
output.add("Done");
}
if (activeID > 8) {
reset();
}
}
void reset() {
output.clear();
word = "";
count = 0;
activeID = 0;
}
void mousePressed () {
doLoop = !doLoop;
}
void keyPressed () {
if (key == ' ') {
doLoop = !doLoop;
} else {
update();
redraw();
}
}
void arrow(int x1, int y1, int x2, int y2) {
line(x1, y1, x2, y2);
pushMatrix();
translate(x2, y2);
float a = atan2(x1-x2, y2-y1);
rotate(a);
line(0, 0, -10, -10);
line(0, 0, 10, -10);
popMatrix();
}
See More Shortcuts