Discussion:
[Tutor] Python help
IDN3
2015-08-13 01:01:12 UTC
Permalink
To whom it may concern,
I am having a problem solving the below question. I have used every
resource that I could find to help me, but I'm seeing nothing. Can someone
out there please help me understand the below question and learn how to
accomplish this task in Python? I would really appreciate any help that
someone could afford.


*Problem 1:* Write a program that will calculate the problem and stop after
the condition has been met.



a=number of loops (start with zero)

b=a+1

c=a+b



Condition: If c is less than 5, then the loop will continue; else, it will
end.



3. *Problem 2:*Print a string variable that states the number of loops
required to meet the condition for Problem 1.

My attempt below. I used a while loop even though the question is saying
IF/THEN/ELSE. To my knowledge loops in Python have to be while/for. Also,
it seems like the question is missing some direction, but I could be
wrong. Thank you for your help.

a = 0
b = a + 1
c = a + b
while (c < 5):
print(c)
c = c + 1
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-13 09:34:49 UTC
Permalink
Post by IDN3
To whom it may concern,
I am having a problem solving the below question. I have used every
resource that I could find to help me, but I'm seeing nothing. Can someone
out there please help me understand the below question and learn how to
accomplish this task in Python? I would really appreciate any help that
someone could afford.
OK, We won;t give you the solution but we can certainly help you
understand the question and point you in the right direction.
Post by IDN3
*Problem 1:* Write a program that will calculate the problem and stop after
the condition has been met.
a=number of loops (start with zero)
b=a+1
c=a+b
Notice that a is the thing that counts the number of loops.
So you need to increment a inside your loop.
b and c are based on a.
Post by IDN3
Condition: If c is less than 5, then the loop will continue; else, it will
end.
3. *Problem 2:*Print a string variable that states the number of loops
required to meet the condition for Problem 1.
This is a simple bit of math.
You don't need to run the loop to figure out the answer,
just write an equation.
Post by IDN3
My attempt below. I used a while loop even though the question is saying
IF/THEN/ELSE.
You obviously did not send the whole question.
I don't see any mention of if/then/else?
Post by IDN3
To my knowledge loops in Python have to be while/for. Also,
it seems like the question is missing some direction, but I could be
wrong.
The two questions are both OK, but i think you missed the bit that said
a was the variable that counted the number of times round the loop.
This is a little bit unusual since normally your while loop test uses
the counting variable (ie. a) but in this case it uses c which is
based on a. But it does demonstrate that the while condition can be more
complex that the basic form.
Post by IDN3
a = 0
b = a + 1
c = a + b
print(c)
c = c + 1
You need to make a the counter and not c.
You need to recalculate c after changing a.
You need to print the number of loops (a) not c.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-13 09:44:32 UTC
Permalink
[...snip...]
Post by Alan Gauld
Post by IDN3
Condition: If c is less than 5, then the loop will continue; else, it will
end.
3. *Problem 2:*Print a string variable that states the number of loops
required to meet the condition for Problem 1.
This is a simple bit of math.
You don't need to run the loop to figure out the answer,
just write an equation.
Maybe so, but for an arbitrarily weird condition running the loop may be the
way to go. Therefore I would imagine he is being asked to print how many times
that loop ran. So he should just print that value after the loop finishes (i.e
outside the loop).
Post by Alan Gauld
Post by IDN3
My attempt below. I used a while loop even though the question is saying
IF/THEN/ELSE.
You obviously did not send the whole question.
I don't see any mention of if/then/else?
Actually, the question's "Condition:" section is written in exactly those
terms.
Post by Alan Gauld
Post by IDN3
To my knowledge loops in Python have to be while/for. Also,
it seems like the question is missing some direction, but I could be
wrong.
The way I would read your "Cndition:" requirement is that it is describing what
kind of decision must be made every time the loop commences. It is not telling
you to use Pythons "if" statement. So just putting the correct condition at the
top of the "while" loop is what you want.

Cheers,
Cameron Simpson <***@zip.com.au>

If you lie to the compiler, it will get its revenge. - Henry Spencer
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-13 10:11:52 UTC
Permalink
Post by Cameron Simpson
Post by Alan Gauld
You obviously did not send the whole question.
I don't see any mention of if/then/else?
Actually, the question's "Condition:" section is written in exactly
those terms.
Oops, so it is. My bad.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...