0

I am supposed to write a function for one of my computer science classes in python. The function is supposed to take in a startValue and then increment it until numberOfValues is reached. This is my function so far:

def nextNValues(startValue, increment, numberOfValues):
   result = int(0)
   for i in range(0, numberOfValues):
      increase = i * increment
       result = startValue + increase
   return result

I call it by doing:

print(nextNValues(5,4,3))

The problem is that the output is only 13. How do I make it so it returns a number each time it increments. For example, 5, 9, 13? I have been having this problem with my previous functions but I have just been adding and removing things without much logic to get it to work. What am I doing wrong?

2
  • Please fix your indentation. Commented Oct 6, 2013 at 20:57
  • 1
    As your class has dealt neither with lists not with generators, are you sure you have to return the values? Maybe your task is to just print the values inside of the function? Commented Oct 6, 2013 at 21:04

3 Answers 3

3

This is a perfect use case for generators.

Long story short, just use yield instead of return:

def nextNValues(startValue, increment, numberOfValues):
  result = int(0)
  for i in range(0, numberOfValues):
    increase = i * increment
    result = startValue + increase
    yield result

The clients of your code can then use it either in a simple loop:

for value in nextNValues(...):
  print(value)

Or they can get a list if needed by converting it with list. For example, if one needed to print the result:

print(list(nextNValues(...)))
Sign up to request clarification or add additional context in comments.

2 Comments

That didn't work. It ended up doing this <generator object nextNValues at 0x1027ebd20>
That's because you tried printing it directly. I showed how to use it. If you want to print it, use a conversion to list.
1

You should build up a list. Each time through the iteration, append the number to the list. Then, at the end, simply return the list.

2 Comments

We haven't really worked with lists yet. The assignment is to do it through just for loops.
There is no way to do this "just through for loops". You need some sort of container to hold multiple values, such as a list. The alternative is to use a generator, as Denis proposes, but that is significantly more advanced - if you haven't even done lists yet, generators are definitely not the expected answer.
0
def nextNValues(startValue, increment, numberOfValues):
    result = []
    for i in range(0, numberOfValues):
        increase = i * increment
        #result = startValue + increase
        result.append(startValue + increase)
    return result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.