-1
program, info = input ("->") .split ()


if program == 'search':
   print (info)
elif program == 'hello':
   print ("do")
else:
   print ("Error")

In this example, the input is split.

E.g.

search youtube.com

Output:

youtube.com

But now I would also like to manage that you can only enter 1 word and it still works.

Such as.

elif program == 'hello':

that means I only type in hello and it works too.

then the output should be:

do

but only one word doesn't work at the moment, how do I get that?

2 Answers2

3

You could add an asterisk to the info variable:

program, *info = input("->").split()

if program == 'search':
   print(info[0])
elif program == 'hello':
   print("do")
else:
   print("Error")

This result in info being a list of everything after the first element of the split input. If you enter search youtube.com, the variable info will contain ['youtube.com']. If you enter hello, the variable info will contain nothing, i.e. [].

Note that I also added a list access to the info variable in line 4, where it is printed.

More information on how to solve this and/or why this works can be found here, where default values in unpacking are discussed.

Edit: As @Steve pointed out, this becomes problematic if only search is entered, because you'd try to access the first element of an empty list. To prevent this, you can add an extra check to your code:

program, *info = input("->").split()

if program == 'search':
   if not info:
      print("Error, nothing to search for")
   else:
      print(info[0])
elif program == 'hello':
   print("do")
else:
   print("Error")
dranjohn
  • 673
  • 7
  • 22
  • 2
    This blows up if the user enters just "search". – CryptoFool Jan 24 '21 at 22:20
  • @Steve Why shouldn't it? Semantically, searching for nothing is invalid. – wjandrea Jan 24 '21 at 22:21
  • @Steve that is correct, but I still feel like it solves OPs problem of unpacking one value into two variables. I'm still going to edit my answer now to add that. – dranjohn Jan 24 '21 at 22:23
  • 1
    Why should it? My belief is that unless otherwise stated, a program should properly deal with any possible user input without throwing an exception that at least looks like a case that the programmer missed. Are you saying that is by your own design? – CryptoFool Jan 24 '21 at 22:23
  • Yes, it should, and no, it wasn't. Thank you for pointing this out. – dranjohn Jan 24 '21 at 22:33
0

Simply check the size of the input before assigning the variables:

data = input("->").split()
if len(data) == 2:
    program = data[0]
    info = data[1]
elif len(data) == 1:
    program = data[0]
else:
    print("invalid number of arguments")
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Downvoter: care to explain? what's wrong with this answer, how would you improve it? – Óscar López Jan 24 '21 at 22:11
  • This does nothing if three or more words are entered. I don't know what the right behavior is. Maybe that's it. I just wanted to point out that that case isn't explicitly dealt with. – CryptoFool Jan 24 '21 at 22:17
  • @Steve the question doesn't mention it explicitly, but anyway I'll add an error message for that case – Óscar López Jan 24 '21 at 23:16