siddharth ingle siddharth ingle - 3 months ago 7
Python Question

If we don't know the number of entries then how to stop a input() loop of while or for

I tried to get input by bigger number entries but it says "index out of range"

if we don't know the number of entries then how to stop a input() loop of while or for, in python:

while():
listMatch.append(input())


Look what if I have:

Djokovic:Murray:2-6,6-7,7-6,6-3,6-1
Murray:Djokovic:6-3,4-6,6-4,6-3
Djokovic:Murray:6-0,7-6,6-7,6-3
Murray:Djokovic:6-4,6-4
Djokovic:Murray:2-6,6-2,6-0
Murray:Djokovic:6-3,4-6,6-3,6-4
Djokovic:Murray:7-6,4-6,7-6,2-6,6-2
Murray:Djokovic:7-5,7-5
Williams:Muguruza:3-6,6-3,6-3


AND

Halep:Raonic:2-6,6-7,7-6,6-3,6-1
Kerber:Raonic:6-3,4-6,6-4,6-3
Raonic:Wawrinka:6-0,7-6,6-7,6-3
Wawrinka:Raonic:6-4,6-4
Halep:Raonic:2-6,6-2,6-0
Wawrinka:Raonic:6-3,4-6,6-3,6-4
Raonic:Wawrinka:7-6,4-6,7-6,2-6,6-2
Wawrinka:Kerber:7-5,7-5
Halep:Kerber:3-6,6-3,6-3
Halep:Wawrinka:0-6,0-6,6-0,6-0,7-5
Kerber:Wawrinka:6-3,4-6,7-6,0-6,7-5


Here I don't know how many entries I want to enter. In above examples there are 9 and 11.

Answer

This loop will stop as soon as you just press return (no string entered)

listMatch=[]

while True:
    z=input()
    if z:
        listMatch.append(z)
    else:
        break