[Tutor] Adding consecutive numbers

Dave Angel davea at davea.name
Mon Apr 27 14:41:58 CEST 2015


On 04/27/2015 06:37 AM, 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.
>

You really shouldn't use attachments on this list, since many cannot see 
them.  I happen to be able to, so I'll try to comment.

In addition, each time you start a new thread, you should specify what 
Python version you're  trying to use, and what OS.

I never heard of an "argumentation error" so you'll really have to show 
the exact error.  Just copy/paste it from your user shell.  And exact 
for syntax errors, show the complete traceback.

Your code has many errors in it, and is unnecessarily complex.  At its 
simplest, you could use some simple algebra, and write:

     x =int(input('Please enter your first number: '))
     y =int(input('Please enter your second number: '))
     print (x+y) * (y-x + 1) / 2

But if you really have to do the sum, then you need to build a list, and 
sum it.  That's not very hard either, since range() returns a list, and 
the sum() function is built-in.

But now, let's look at your code, and identify a few of the problems 
with it.

Your second line calls function interact(), which isn't defined yet. 
And in fact, when it is defined, it is spelled differently.  Put your 
top-level code (those two lines, anyway) at the *end* of the file, after 
the function(s) it needs are defined.

In function iteract (spelled inconsistently), you have a while True 
loop, with a break in the else clause.  But if x<y, then you'll loop 
forever, since neither one changes, and if x>=y, you'll hit the break 
without defining z, so print z will throw an exception.

Also in that function you call a function called adding_sum_in(), but 
there's no function by that name.


In function find_range(), the first thing you do is trash your 
arguments.  So whatever else is in that function probably doesn't matter 
much.

In the loop:
             for num in x:
                 x.append(num)

you're modifying the same thing you're looping over.  Not generally a 
good idea, so I have no idea if it'll do anything useful or not. 
Probably not.

     return R = range({0},{1}).format(x,y)

is a syntax error.

def adding_sum_in_range(x, y):
     c= find_range(x,y)
     Total_sum = 0
     for i in c:
         Total_sum += i
         return Total_sum

You have an indentation problem there, as this will return the first 
time through the loop.

def addition(x, y):
     num1=(x)
     num2=(y)
return num1+num2

Another indentation problem.

-- 
DaveA


More information about the Tutor mailing list