-1

I want to use one line to get multiple inputs, and if the user only gives one input, the algorithm would decide whether the input is a negative number. If it is, then the algorithm stops. Otherwise, the algorithm loops to get a correct input.

My code:

integer, string = input("Enter an integer and a word: ")

When I try the code, Python returns

ValueError: not enough values to unpack (expected 2, got 1)

I tried "try" and "except", but I couldn't get the "integer" input. How can I fix that?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
user398843
  • 159
  • 8

2 Answers2

2

In order to get two inputs at a time, you can use split(). Just like the following example :

x = 0
while int(x)>= 0 :
    try :
        x, y = input("Enter a two value: ").split() 
    except ValueError:
        print("You missed one")
    print("This is x : ", x) 
    print("This is y : ", y) 
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
MelKoutch
  • 180
  • 1
  • 12
  • You mean it does not work when you input a single value? What is the error you are getting now? – Mortz Mar 25 '19 at 15:27
  • @user398843 Do you mean like a while loop that checks the x value ? – MelKoutch Mar 25 '19 at 15:28
  • I think while loop does not work. If you only provide one value to your algorithm, you get an error, so you cannot use while loop to check. – user398843 Mar 25 '19 at 15:31
  • Check out the edit, tell me if that fits what you need. The program keeps asking the user for two values but stops when the first one is negative. – MelKoutch Mar 25 '19 at 15:33
  • If I give only one input, the it still returns ValueError: not enough values to unpack (expected 2, got 1). And this is why I'm seeking a help – user398843 Mar 25 '19 at 15:38
  • Why do you give only one input ? Give it two and if the first one is not negative it will keep asking. – MelKoutch Mar 25 '19 at 15:39
  • 1
    That's a question from my professor.. I wish I could decide what the algorithm would be – user398843 Mar 25 '19 at 15:41
  • I used a try/except to handle the exception however that still requires two values in input. – MelKoutch Mar 25 '19 at 15:46
  • Check if you can convert one of the inputs to an int/float by try/except. After that assign the values. If you can't convert it, it must be just a string... – user2853437 Mar 25 '19 at 15:52
  • You should move the `print`s inside the `try`. If there were not enough inputs, the `print` lines might raise an error – Tomerikoo Oct 19 '20 at 09:26
0

I believe the implementation below does what you want.

The program exits only if the user provides a single input which is a negative number of if the user provides one integer followed by a non-numeric string.

userinput = []

while True:
    userinput = input("Enter an integer and a word: ").split()
    # exit the program if the only input is a negative number
    if len(userinput) == 1:
        try:
            if int(userinput[0]) < 0:
                break
        except:
            pass
    # exit the program if a correct input was provided (integer followed by a non-numeric string)
    elif len(userinput) == 2:
        if userinput[0].isnumeric() and (not userinput[1].isnumeric()):
            break

Demo: https://repl.it/@glhr/55341028

glhr
  • 4,439
  • 1
  • 15
  • 26