0

I am trying to solve some problems in CodeAbbey using Python.I have run into a wall trying to take input for these programs.I have spent so much time analyzing how to take the input data without even solving the question.Hope someone explains how to take input.

Problem:I have to input the following numbers in One go. I have tried using 'input()' but it takes only one line. Is there any work around to do it in a simple way? i wasted so much time trying to analyse various options

632765 235464
985085 255238
621913 476248
312397 751031
894568 239825
702556 754421
474681 144592

You can find the exact question here: http://www.codeabbey.com/index/task_view/sums-in-loop

zwer
  • 24,943
  • 3
  • 48
  • 66
agastya teja
  • 629
  • 2
  • 9
  • 18

2 Answers2

0

My first attempt would be to try a typing like "632765 235464\n985085 255238[...]" so you could read it as one line. This would be pretty hacky and not a good idea if its real userinput.

The other idea: Why not taking the input line by line and putting these lines in a list / appending them to a string?

EDIT:

I found some Code on SO, but its python2.7 i guess. ->Here

The Python3.X style would be:

#!/usr/bin/python
input_list = [] 

while True: # Infinite loop, left by userinput
    input_str = input(">") #The beginning of each line.
    if input_str == ".": #Userinput ends with the . as input
        break # Leave the infinite loop
    else:
        input_list.append(input_str) #userinput added to list

for line in input_list: #print the input to stdout
    print(line)

Hope this will help :)

Simulacrum
  • 96
  • 1
  • 1
  • 9
  • Thanks ,for the first solution you provided: i didn't want to hardcode the numbers.for the next solution, what if there are 100 lines, i have to enter them 100 times . hence i ruled out that. Is there any other way, please! – agastya teja Feb 24 '17 at 05:15
  • I just edited this answer. Maybe this will help (i have no clue if you'd get a notification if i edit smth) – Simulacrum Feb 24 '17 at 05:17
  • I viewed this solution previously man, couldn't understand!:/. As i am beginner , i want a simpler method to comprehend or can you explain this code? – agastya teja Feb 24 '17 at 05:22
  • Yeah, i guess i can try to explain. This code reads the input in an infinite loop, until the user inputs a "." character. (you can remove everything after if input_str == "."). so, if the input is not a dot, the code appends the input line to a list. I hope this helps, i just woke up ^^ – Simulacrum Feb 24 '17 at 05:35
  • I added comments to the code. this should be better to understand. -> You can now C&P the numbers. – Simulacrum Feb 24 '17 at 06:04
  • hi i understand where you are coming from, but what if the user has more inputs? – agastya teja Feb 24 '17 at 06:54
  • for eg, here its takes 15 inputs. I should enter each input manually. I thought of using Ctrl – agastya teja Feb 24 '17 at 06:59
  • *copy and pasting the all the 15 at once – agastya teja Feb 24 '17 at 06:59
  • I tried it in my IDE (PyCharm) and it seems to work perfectly fine :) – Simulacrum Feb 24 '17 at 07:22
  • @agastyateja Yo mate, keep us updated. Did it work for you? – Simulacrum Feb 24 '17 at 20:42
  • I actually found a work around. I copied those data into a file. Opened the file in Read mode and used split lines. Easy peasy lemony squesy! – agastya teja Feb 25 '17 at 04:37
0

You can just repeat input() until you get all your data, e.g.:

try:
    input = raw_input  # fix for Python 2.x compatibility
except NameError:
    pass

def input_pairs(count):
    pairs = []  # list to populate with user input
    print("Please input {} number pairs separated by space on each new line:".format(count))
    while count > 0:  # repeat until we get the `count` number of pairs
        success = True  # marks if the input was successful or not
        try:
            candidate = input()  # get the input from user
            if candidate:  # if there was some input...
                # split the input by whitespace (default for `split()`) and convert
                # the pairs to integers. If you need floats you can use float(x) instead
                current_pair = [int(x) for x in candidate.split()]
                if len(current_pair) == 2:  # if the input is a valid pair
                    pairs.append(current_pair)  # add the pair to our `pairs` list
                else:
                    success = False  # input wasn't a pair, mark it as unsuccessful
            else:
                success = False  # there wasn't any input, mark it as unsuccessful
        except (EOFError, ValueError, TypeError):  # if any of the conversions above fail
            success = False  # mark the input as unsuccessful
        if success:  # if the input was successful...
            count -= 1  # reduce the count of inputs by one
        else:  # otherwise...
            print("Invalid input, try again.")  # print error
    return pairs  # return our populated list of pairs

Then you can call it whenever you need number pairs like:

my_pairs = input_pairs(7)  # gets you a list of pairs (lists) entered by user
zwer
  • 24,943
  • 3
  • 48
  • 66
  • Please man, i am a beginner! and looking at this scared me away.Should i do this much just to enter the data? i haven't even solved the problem yet. – agastya teja Feb 24 '17 at 05:56