0

I'm a beginner to Python and I'm having some trouble with this. I have to make a for loop out of this problem. Can anyone explain how I would go about this?

nextNValues (startValue, increment, numberOfValues)

This function creates a string of numberOfValues values, starting with startValue and counting by increment. For example, nextNValues (5, 4, 3) would generate a string of (not including the comments):

5 - the start value
9 - counting by 4, the increment
13 - stopping after 3 lines of output, the numberOfValues

5
  • 1
    You could use range(startValue,startValue+(increment*numberofValues),increment) Commented Oct 6, 2013 at 5:09
  • @SrinivasReddyThatiparthy add that as an answer and I'll remove my answer :) Commented Oct 6, 2013 at 5:12
  • Do you want a string? Or do you want to print the numbers to the console? Or do you want a list of numbers? Commented Oct 6, 2013 at 5:12
  • I can't use a print statement inside the function. So I have to use a return statement and then create a "main" function where I would print the output. this is what I got so far: Commented Oct 6, 2013 at 5:17
  • def nextNValues (startValue, increment, numberOfValues): result = 0 for i in range (numberOfValues): result = startValue + i * increment return increment Commented Oct 6, 2013 at 5:18

3 Answers 3

3

You could use range(startValue,startValue+(increment*numberofValues),increment).

Sign up to request clarification or add additional context in comments.

2 Comments

+1 also you can use xrange and produce a generator (where the sequence is generates as each next element is accessed instead of all at once like this (this is good if you want a LONG sequence)
there is no xrange in python3.range=xrange in python3. :)
2
for i in range(numberOfValues):
    print startValue + i * increment

I am not sure if that is exactly what you are looking for... but it is my suggestion based on the information you have posted.

1 Comment

this was a huge help! Thank you very much sir!
0

It would probably be easiest to write a for loop with an index like i and use that to add i*increment ti start value and save the resulting value to a list. Have the loop run numberOfValues times. If this is homework it would be better for you to write out the actual code for yourself

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.