Python Forum
making a program for counting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a program for counting
#1
Can anyone help me make a simple program.
I want to make a program using while function to count how many bars of soap i have in rows. First row would have 1 bar, second row has 2 bars, third one has 3 bars and so on.
I tried to do it whith while function but i cant make it count them right.
Reply
#2
Show us what you tried, and we'll help you fix it. Post your code using Python tags (see the BBCode link in my signature below for instructions).

Note that while is a statement, not a function. Statements are more restricted as to where they can be used.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This is the code i have right now
Text = input("How many rows of soap")
I = int(text)
Sum = 0
While i<0:
Sum = sum + 1
Print(sum)
I = i + 1
Reply
#4
There are several problems here. First is that you didn't use python tags like I asked you to. Indentation is important in python, and without those tags I can't see what your indentation is. Assuming the indentation is correct, the second problem is that capitalization matters. The variable Text is not the same as the variable text, and the variable I is not the same as the variable i. I is a bad name for a variable, anyway. Try something more descriptive, like rows. The third problem is the condition i < 0. I is probably going to be positive when entered, and you increase it by one each time through the loop. Therefore it will never be less than zero. You probably want to decrease it each time through the loop, and test that it is greater than zero. Finally, you are adding the wrong thing to sum.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
just to add that it is while not While
Reply
#6
Sorry im using my phone right now so i couldnt get to my python version. But ill give it a go when i get home amd see if that works.
Reply
#7
Isn't that just n(n+1)/2, where n is the number of rows? No loop needed, or am I missing something here?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#8
(Oct-01-2017, 04:36 PM)gruntfutuk Wrote: Isn't that just n(n+1)/2, where n is the number of rows? No loop needed, or am I missing something here?

Maybe that we are in Homework section and most of the time there are specific requirements - use this, don't use that, based on material that has been covered in class/course or what the teacher want to emphasize. Apart from that you are right that it's possible to use formula for sum of arithmetic progression. Not to mention that python has sum built-in function that can be applied over sequence/range. :-)
Reply
#9
(Oct-01-2017, 05:06 PM)buran Wrote:
(Oct-01-2017, 04:36 PM)gruntfutuk Wrote: Isn't that just n(n+1)/2, where n is the number of rows? No loop needed, or am I missing something here?

Maybe that we are in Homework section and most of the time there are specific requirements - use this, don't use that, based on material that has been covered in class/course or what the teacher want to emphasize. Apart from that you are right that it's possible to use formula for sum of arithmetic progression. Not to mention that python has sum built-in function that can be applied over sequence/range. :-)
Oops - yup. Was thinking it too trivial ... but you are right, of course; just because there is an easy way, doesn't mean it isn't useful for learning a simple technique.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Making maths .py program faster Shutcois 1 2,540 Feb-16-2018, 06:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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