is anyone a programmer (python)?

Welcome to the Precious Metals Bug Forums

Welcome to the PMBug forums - a watering hole for folks interested in gold, silver, precious metals, sound money, investing, market and economic news, central bank monetary policies, politics and more. You can visit the forum page to see the list of forum nodes (categories/rooms) for topics.

Why not register an account and join the discussions? When you register an account and log in, you may enjoy additional benefits including no ads, market data/charts, access to trade/barter with the community and much more. Registering an account is free - you have nothing to lose!

oppie2005

Predaceous Stink Bug
Messages
129
Reaction score
1
Points
0
im trying to learn programming, and the tutorial that i am using has been pretty good, but has exercises at the end of each lesson. For the most part, im doing pretty decent with the exercises, but could use a little help on some of them.....if anyone is fluent....
 
I'm a programmer, but I have no experience with Python. If you have questions of a general nature, I can probably help.
 
ok. i dont know if you can help or not, cause im inexperienced, and dont know how similar the "problem" is with other languages.

the exercise says to define a function to give the sum of of all integer numbers up to and including n using the accumulator pattern. the example earlier in the tutorial for the accumulator pattern was for calculating the square root of n.

an earlier exercise was to define the same "sumTo" function, using a different way to calculate it

def sumTo(n):
y = (n * (n+1))/2
return y

Im understanding the accumulator pattern to mean updating a variable for each cycle through a for loop that would add up each number in the range of n, instead of in the example above where multiplication and division is used.

does my ramblings make any sense? i think i have confused myself even further in this process lol.


[edit:]the tutorial that i am using is found here: http://interactivepython.org/courselib/static/thinkcspy/Functions/functions.html

where it talks about the accumulator pattern, is about halfway down the page, and the exercise that im working on is near the very bottom of the page.
 
Last edited:
In SAS, this would look something like this:

%let maxnum=13;
%macro data_mining;
%do i=1 %to &maxnum;
def sumTo(i):
y = (i * (i+1))/2
%end;
%mend;
%data_mining;


Sorry, I have used a lot of languages, but not Python.
 
Not sure I fully understood what is meant be "accumulator pattern", but you have two basic choices for calculating the sum of integers in a range from 0..n - an iterative loop (for/next, while/wend, etc.) or a recursive function (likely not the approach they are looking for in an introductory programming course). It would look something like this:
def AddOneToN(n)

lSum = 0
FOR lLoopCounter = 1 TO n
lSum = lSum + lLoopCounter
NEXT

return lSum
 
pmbug, you, sir, are a genius.

from your example i typed

def sumTo(n):
----runningtotal = 0
----for counter in range(n +1):
--------runningtotal = runningtotal + counter

----return runningtotal

and that was exactly what i was trying to do. i spent an hour or two trying to figure it out last night.


btw, PMBug, what language are you using for that example?
 
Last edited:
It's "psuedo code". I tried to mimic your code sample. The "for ... next" loop is BASIC style.
 
Back
Top Bottom