Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incremental loop?
#1
Hello,

I am a beginner student and this is pretty basic, but I am stumped. Our text is Python for Dummies and I am having a hard time figuring this loop out. We jumped from doing pseudocode and flowcharts and basic problem to this loop. The loop is this:

a = number of loops (starting with 0 but increasing by 1 each iteration)

b = a + 1
c = a + b

Condition if c < 5 then loop will continue, else, it will end


So I defined a = 0 but realized that just gives me the variable. Then I defined a and gave it a range of 0, 5. But wasn't sure how to get that range to run automatically. Part of my problem is on the Python tutorials I am confused by the way they do loop examples. I'm basically totally confused. I would appreciate any clarification.
Reply
#2
What edition of Python for Dummies you are using? Which chapter it is?

In Python there are for statements and while statements (you can nest them as well). These are keywords which make loop 'run'.

Maybe code below can give you some understanding:

>>> a = 5            # number of loops
>>> while a > 0:     # condition when loop is running
...     print(a)     # what to do on every iteration
...     a -= 1       # decrease remaining number of loops on every iteration
...
5
4
3
2
1
while-loop runs while condition is True (or break is encountered in body), it's like plain english: "while a is larger than zero print value of a and then decrease value of a by 1". So first loop is a = 5, condition is met and it's printed, then a becomes 4, condition still holds and 4 is printed and so forth until a is 0. Then condition doesn't hold anymore and loop finishes automagically Smile
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
perfringo,

Thank you for taking the time to respond. We are using the 2014 edition and are basically covering parts I-III currently. What you said makes sense, and sorry for not clarifying but we have covered while loops and for loops and the loop I need to do is a while loop I believe. I guess maybe I was confused about how Python works. You write a piece of code like the one you did, and when you run it, it will execute? I was thinking there was some other piece of code to write to specify a loop. Also, how would I write a string variable to state the number of loops I made?

I guess the loop I wrote out is pretty simple and I can just write it out...thanks for the help.

>>> a = 0
>>> b = a + 1
>>> c = a + b
>>> while c < 5:
	print(a)
	a += 1
	if c > 5:
		break
So I wrote this, but my break appears to be ineffective and the loop just continues endlessly. What did I express wrong?
Reply
#4
Your definitions of b and c don't define a relationship with a, they just use the value of a at that point in time to set values for b and c. That means that c never changes in your loop. You need to add those formulas for b and c to your loop so that they update their values each time through the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Hi, I know that my post is a few days late and you've probably figured it out by now, but I don't see your reply here so I'll just chime in.

(Mar-12-2019, 11:19 PM)Headphone256 Wrote: I guess the loop I wrote out is pretty simple and I can just write it out...thanks for the help.
Python Code: (Double-click to select all)
1
2
3
4
5
6
7
8

>>> a = 0
>>> b = a + 1
>>> c = a + b
>>> while c < 5:
    print(a)
    a += 1
    if c > 5:
        break
So I wrote this, but my break appears to be ineffective and the loop just continues endlessly. What did I express wrong?

You aren't incrementing the value of c at all, only a. Your loop will run infinitely due to this, it checks for c to be less than 5, but c will always have the value of a + b, aka what you initialized it as before the while loop. If you want to change the value of c then you would have to throw that variable in there. Right now all it's doing is just assigning c to the value of 1.

Look at this example:
# initialize a to value 0
a = 0
# while a is less than 5
while a < 5:
    # print value of a
    print(a)
    # check if a is greater than 5
    if a > 5:
        # if it is, then break out of loop
        break
    # increment a by 1
    a += 1
Reply
#6
(Mar-12-2019, 11:19 PM)Headphone256 Wrote: So I wrote this, but my break appears to be ineffective and the loop just continues endlessly. What did I express wrong?

As ichabod801 already explained:

>>> c = a + b   # value of c is 0 + 1 = 1 and it is not changing when a changes
You changing value of a and therefore you are in endless loop - c is and remains 1 and while condition is always True

And of course, there is no need to break out with same condition as while-loop. If conditions becomes False (c is not less than 5) body of loop is not run anymore and Python moves to next code block; no need for additional internal break with similar condition.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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