Python Forum

Full Version: Infinite # of inputs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody,
How can I put an indefinite number of inputs in a program?
Let me explain myself better: (here is the code)
'''ex 8
def multiplier(items):
    multiplied=[]
    multiplied=[item*2 for item in items]
    return(multiplied)
try:
    number1=int(input("Insert 5 numbers (1st): "))
    number2=int(input("Insert 5 numbers (2nd): "))
    number3=int(input("Insert 5 numbers (3rd): "))
    number4=int(input("Insert 5 numbers (4th): "))until a precise input, (I.E: "stop")?
For example, user Jimmy inserts:
    print(multiplier([number1, number2, number3, number4]))
except ValueError:
    print("insert a number!")
'''
How can I make the program accept as many inputs as the user wants until a precise input, for example ("stop")?
Another pretty straight-forward example:
2,
4,
8,
4,
-3,
"stop"
The program will multiply (in this case) 2, 4, 8, 4 and -3.
I don't know if I made this clear enough tho, sorry.
Anyway, thanks in advance for your kind help! ( Oh, and if you could tell me how to improve the code in general, that'd be appreciate :) )
Use a while
## meant to be an example only
user_input=""
while user_input.lower() != "stop":
    user_input=input("Enter another number ")
    print(user_input)