0

I am trying to create a function that calculates the sum of an artithmetic sequence. I know how to set up the mathematical calculations but I don't know how to take input from the user to actually perform them.

How can I take user input (like below) such that the three ints on each line are read as A, B, N, with A being the first value of the sequence, B being the step size and N the number of steps.

8 1 60
19 16 69
17 4 48

What should come next?

def arithmetic_progression():
    a = raw_input('enter the numbers: ')
lesmuse
  • 19
  • 4
  • 3
    Take a look at http://stackoverflow.com/questions/11404946/accepting-multiple-user-inputs-seperated-by-a-space-in-python-and-append-them-to – PM 2Ring Nov 27 '15 at 11:33

2 Answers2

2

with raw_input you generally get a string

>> a = raw_input('enter the numbers')

you enter the numbers 8 1 60, so a will be a string '8 1 60'. Then you can split the string into the 3 substrings

>> b = a.split()

This will return you a list ['8', '1', '60']. Out of this you can get your numbers

>> A = int(b[0])
>> B = int(b[1])
>> N = int(b[2])

To read multiple lines you could add a function similar to this

def readlines():
     out = raw_input('enter the numbers\n')
     a = 'dummy'
     while(len(a)>0):
         a = raw_input()
         out += '\n' + a
     return out

This function would read any input and write it to the out string until you have one empty line. To get the numbers out of the string just do again the same as for a single line.

Rolf Lussi
  • 615
  • 5
  • 16
  • There are multiple lines of input and I can only make the above method work for a single line. When reading from a text file the readlines() method solves my problem. However when dealing with user input Python treats muliple lines as all one string. Is there any way to perform a readlines() like function on user input similar to what I posted in the question? – lesmuse Nov 27 '15 at 22:58
  • Well, generally `raw_input` reacts on the newline to terminate it. So if you want to read multiple lines you can call the `raw_input` again, you can call it without text, so the user doesn't even see its a new input and not the same again. I'll add the function to the answer. – Rolf Lussi Nov 28 '15 at 13:33
0

Sum to n terms of AP is: Sn = (n/2) [ 2a + (n-1)d ]

def arithmetic_progression():
    inp = raw_input('enter the numbers: ').split(' ')
    if not len(inp) == 3:               # in case of invalid input
        return arithmetic_progression() # prompt user to enter numbers again

    a = float(inp[0])
    d = float(inp[1])
    n = float(inp[2])

    s = ( (2 * a) + ((n - 1) * d) ) * (n / 2)

    print('Sum to n terms of given AP is: ' + str(s))

arithmetic_progression()
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • 1
    1). Please don't use recursion when a simple loop will do. Recursion in Python isn't efficient (it cannot perform tail call elimination), and the Python interpreter imposes a limit on the depth of recursive calls. 2). If you're going to loop on invalid input it's probably a Good Idea to do more than simply test that the user supplied 3 values: you should check that they're valid numbers, too. There are some good examples in [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/4014959). – PM 2Ring Nov 27 '15 at 11:42
  • 1
    Also, I would like to add that if you already separated this feature as a function, then it would be nicer to separate IO from engine logic. – Lajos Arpad Nov 27 '15 at 11:45