Hi everyone it's been almost a year since I gave up programing in python so I have forgotten a lot of things, so just bare with me. Ok so I was playing around with python and ended up with this little program.

def test():
	number = input("Number: ")
	scale = range(1, number+1)
	for count in scale:
		print (count) * 20

Ok so what this program does is it ask you for a number, then it makes a scale from one to whatever number you type in. Then it multiply's every single number in the scale by 20(randomly picked) and gives and output. That works perfectly now what I want to know is if there is a way to add up all the results and get one final answer. Thank you in advance for taking your time to read this:)

Use sum

def test():
    number = input("Number: ")
    return sum([count*20 for count in range(1, number+1)])

jlm: You're using a little more than just sum() in that. Why didn't you at least name what the [] is? ( I never use it, and I don't have a reference handy... )

The other way that you could have done it is to increment a variable by the number of another variable, which you assign (count)*20. Tell me this, though... How is it random? I would think that it would be a sequence from 1 to whatever number you type in +1...

thanks that helped a lot

Zetlin, your code was almost there:

def test():
    number = input("Number: ")
    scale = range(1, number+1)
    total = 0
    for count in scale:
        n = count * 20
        print n
        total += n
    return total

total = test()
print "Total sum =", total

And if you want to follow along with your first idea, you have to return the total if you want to access it outside the function.

def test(l):
    total = 0
    number = input("Number: ")
    for count in range(1, number+1):
        value = (count) * 20
        print "value =", value
        total += value
    print "The total =", total
    return total

## for what it's worth, since you multiply by 20, you can just add 
## up the range and multiply that by 20 as well.

jlm: You're using a little more than just sum() in that. Why didn't you at least name what the [] is? ( I never use it, and I don't have a reference handy... )

That was a list comprehension, which produces a list. Instead of using his entire for loop I cut it down into a single-line list comprehension. These are a great thing to learn when you're looking to simplify for loops.

Here's a for loop that we'll shorten:

>>> items = ['a','1','b','2','c','d','3','e']
>>> new_items = []
>>> for item in items:
...     if not item.isdigit():
...         new_items.append( 'Modified: %s' % item )
...     
>>> new_items
['Modified: a', 'Modified: b', 'Modified: c', 'Modified: d', 'Modified: e']
>>>

Now with the list comprehension:

>>> items = ['a','1','b','2','c','d','3','e']
>>> new_items = ['Modified: %s' % item for item in items if not item.isdigit()]
>>> new_items
['Modified: a', 'Modified: b', 'Modified: c', 'Modified: d', 'Modified: e']
>>>

Hopefully that's clear enough that you can follow the basic idea. If you don't understand I can try to break it down and explain each step

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.