Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite # of inputs
#1
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 :) )
Reply
#2
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) 
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020