+ 1
Python multiple inputs on a line
How do I take 3 inputs on a line that the program will still run if 1 input was provided
39 Answers
+ 1
hobbies = list(input().split())[:3]
try:
x,y,z = hobbies
except:
for i in range(3-len(hobbies)):
hobbies.append(None)
x,y,z = hobbies
print(' '.join(map(str,[x,y,z])).replace('None',''))
Try this
If you would like a better solution tell me exactly what is the problem you are trying to solve by this program .
Check it out here and give as many inputs you may like 0-infinite:
https://code.sololearn.com/cxGsfiRnNOoM/?ref=app
+ 1
Um.... i think he wants to store the first 3 so that he can do something. like he wants to do something with those x , y and z but ignore the extras for now and also he is giving highest priority to the first element . this is just my assumption according to his descriptions and replies.
+ 1
Bob_Li well meanwhile my program :
1) He needs 3 optional inputs
2) He needs to store them
3) If they are not provided make them None
4)If a lot are provided just ignore them
Meanwhile yours :
1) I just take 1st input into consideration and store it
bro why do you forget he said , he needs 3 input in a single line (space seperated) and if only 1 , 2 or 3 or none or more are given the program should still run . But you are forgetting the main point
+ 1
Yes , haven't you seen my code it says right at the end that i don't know why someone will need it that way but we have to make what we are asked to 😂
+ 1
we just like to climb imaginary mountains. Or set artificial constraints.
+ 1
Anonymous_User Adekunle Ojekunle
perhaps it is not about Sololearn input?
Maybe he means function that can take arbitrary number of arguments?
Then maybe something like this?
def print_arguments(*args):
for a in args:
print(a)
print_arguments(1, 'fdgf', '😎')
print_arguments(1, 'fdgf')
print_arguments(1)
print_arguments()
+ 1
Bob_Li we should just leave this topic because " Perhaps there are infinitely many possibilities about what he means " 🤣
0
Input with space separated..
After reading split into 3 with space..
0
It won’t run if i use one input, it says (expected3, got 1)
0
Can you share your try here?
0
hobbies, y,z=input(“Enter hobbies:).split()
Thats my code, if i input one input, it wont run because its expecting 3 inputs instead of 1, i dont know how to get it to run with only one inputs
0
Adekunle Ojekunle
your are using wrong quotes..
hobbies, y,z= input("Enter hobbies:").split()
print(hobbies ,y,z)
0
Means? What not way?
0
It was a mistake, if you input only one, it wont run. Thats what i’mtrying to figure out
0
If i input one value, the program won’t run because its expecting 3 inputs not 1
0
Do you need to run on any inputs of 1 to 3 ?
Can you tell me what the remaining task with the code ?
0
Yes, i need to run on any inputs, it just has to print any input that is provided either 1,2 or 3
0
You can use this:
hobbies = input("Enter hobbies:").split()
print(*hobbies) #unpacking into
#if you must set 3 variables to default then use try catch..
edit:
Adekunle Ojekunle
do you aware of try - except blocks?
0
while True:
try: print(input())
except EOFError: pass
0
سلام