[Tutor] Adding consecutive numbers
Steven D'Aprano
steve at pearwood.info
Mon Apr 27 15:45:10 CEST 2015
Hi, and welcome!
My answers are below.
On Mon, Apr 27, 2015 at 08:37:54PM +1000, Whom Isac wrote:
> Hi, I am trying to build a python mini program to solve a math problem . I
> wanted my program to ask two number from the operator and it can add those
> number as a integer but as consecutive number in range. For example, if
> num1 entry =1 & num2 entry = 100 , the program should be adding number from
> 1 to 100 together(which should equal to 5050). I thought that I could use
> range() to use it for any given input from the user. However, there are few
> argumentation error and I don't know why my bool function did not work in
> while loop. Is there any way to make the program more concise.
> I have uploaded my code file below. Please, note that I am fairly new to
> programming and donot use complex modules, therefore, suggest me where I
> was wrong with the code. I have checked for indentation error, however,
> there were no error. So, I don't have any idea about the error.
> Thanks.
Thank you very much for sending the code you have so far! Unfortunately,
I wish I had good news but I don't -- nearly everything about it is
wrong.
You have a good idea of what you want the program to do, and you have
made a good attempt at splitting it into separate functions, but sadly
the details are wrong and that prevents your code from working. Let's
see if we can fix that.
The first, and most important, thing is that Python can only run code it
has already seen. So if I write this:
do_something() # Call the function.
def do_something(): # Define the function.
print("Working really hard...")
it will fail because at the time Python tries to call do_something() it
doesn't exist yet. This is your very first problem:
> if __name__=='__main__':
> interact()
>
> def iteract():
When you try to call interact(), the function doesn't exist!
Rule 1: Put your function definitions at the top of the file. Put the
if __name__ == '__main__' block at the bottom of the file. That way, by
the time Python reaches that block and calls the functions, they will
have already been created and are ready to use.
Now, let's think about good program design. A well-designed program will
separate the program logic from the user-interface. In other words, the
part of the program which calculates the result should be kept apart
from the part of the program which communicates with the user. That
makes it much easier to test each half separately, in case of any
problems.
So, let's start with a simple function that takes two integers as
arguments, and adds all the numbers between then. Python gives you the
tools to make this easy:
---- cut here -----
def add_range(start, end):
"""Add the numbers between start and end inclusive."""
the_range = range(start, end+1)
return sum(the_range)
print(add_range(1, 7)) # Should print 28
print(add_range(8, 20)) # Should print 128
---- cut here ----
There is a function, sum(), which is used for adding up a sequence of
numbers, and another function, range(), which creates a sequence of
numbers from start to one less than the end. So we add one more to the
end, create the range, then use sum() to add up the numbers.
Now let's have another function to get a number from the user. We will
use that function to get the starting number, then re-use it to get the
ending number, then finally pass those two number to the add_range()
function we created above.
---- cut here ----
def get_user_number(message):
"""Get an int from the user."""
while True:
# Keep trying forever.
answer = input(message)
if answer.isdigit():
return int(answer)
else:
print("Sorry, that is not a number!")
start = get_user_number("Please enter your starting number: ")
end = get_user_number("Please enter your ending number: ")
print("start is", start, "; end is", end)
print(add_range(start, end))
---- cut here ----
And that's nearly all you need!
Any questions?
--
Steve
More information about the Tutor
mailing list