-3

I need to input an unknown number of rows (it can be a singular row once and then 432 of them the other time). I tried using the while loop which I think would be good for this type of program, but the only solution I can find is if I input an empty row after the ones with data and I need a program which doesn't contain that (since that is the way examples work)

a=[]
b=[]
l=[]
while True:
    l = list(input().split())
    if l[0] == ' ':
        a.append(l[0])
        b.append(int(l[1]))
    else:
        break

My inputs look like this:

T 20

This is the program that I tried to use, it has been published here a bunch of times;

I'm new to programming and trying to get into the world of it so I'm hoping for some help here :)

Borisonekenobi
  • 469
  • 4
  • 15
pythonno
  • 1
  • 2
  • As it stands, it's not entirely obvious what the problem is. What is the text given in `input()` and what result do you want? – Peter Jul 21 '23 at 14:09
  • How would the program know that the input has ended? Does the input have to be typed into the console, or can it come from a file? – Pranav Hosangadi Jul 21 '23 at 14:09
  • The text given would be a letter and a number ex. "T 20". I would want a letter to be a part of the list 'a' and the number to be a part of the list 'b'. The problem is, there can be as much or as little inputs, as I said, an unknown number, and when the 'text' input ends (ex. "T 20") there wouldn't be an empty row added behind it so I don't know how the programme is supposted to know that the input ended. (It's a task from previous programming competitions, not made up) @Peter – pythonno Jul 21 '23 at 14:15
  • May be a duplicate of https://stackoverflow.com/questions/1450393/how-do-i-read-from-stdin – rochard4u Jul 21 '23 at 14:21
  • Does this answer your question? [How do I read from stdin?](https://stackoverflow.com/questions/1450393/how-do-i-read-from-stdin) – rochard4u Jul 21 '23 at 14:23
  • You are defining `l` as `['T', '20']`, then checking if the first element is an empty space. There's no "empty row" added, it's just resulting in empty lists because the if statement never succeeds. – Peter Jul 21 '23 at 15:47

1 Answers1

0

Your wording has me a bit confused, but here are a couple of possible solutions to your problem:

Reading from File

If your input is coming from a file, and the file doesn't have an empty new line at the end, you can simply iterate through all the lines in the file similar to this:

a = []
b = []

with open('path/to/file.ext') as input_file:
    for line in input_file:
        l = line.split(' ')
        a.append(l[0])
        b.append(int(l[1]))

User Input

For Loop

If your input is instead coming from the terminal window, and you don't want to have the user enter an empty line at the end, you can instead do something like this:

numLines = int(input('Enter the number of inputs: '))

a = []
b = []

for i in range(0, numLines):
    l = input().split(' ')
    a.append(l[0])
    b.append(int(l[1]))

While Loop

Finally, if you'd like to keep using while loops instead, you can do a similar solution to the one above:

numLines = int(input('Enter the number of inputs: '))

a = []
b = []

i = 0
while i < numLines:
    l = input().split(' ')
    a.append(l[0])
    b.append(int(l[1]))
    i += 1

or just have the user input an empty line like this (very similar to your code):

a = []
b = []
l = []

while True:
    l = input().split(' ')
    if l[0] == '':
        break
    else:
        a.append(l[0])
        b.append(int(l[1]))

I hope one of these solutions helped! :)

Borisonekenobi
  • 469
  • 4
  • 15