Posts: 3
Threads: 1
Joined: Sep 2017
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 3
Threads: 1
Joined: Sep 2017
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 8,169
Threads: 160
Joined: Sep 2016
just to add that it is while not While
Posts: 3
Threads: 1
Joined: Sep 2017
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.
Posts: 232
Threads: 2
Joined: Sep 2017
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
Posts: 8,169
Threads: 160
Joined: Sep 2016
Oct-01-2017, 05:06 PM
(This post was last modified: Oct-01-2017, 05:06 PM by buran.)
(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. :-)
Posts: 232
Threads: 2
Joined: Sep 2017
(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
|