Home > OS >  I need to re input if the user doesnt't input 3 values
I need to re input if the user doesnt't input 3 values

Time:12-02

I have this code

n,h,y=input("Enter three values: ").split(" ")

If user input 1 value or 2 values there is an error (not enough values to unpack (expected 3, got 1) How to re input if user make this error? using while True and if statement?.

CodePudding user response:

ِYou can use the below code:

n, *others = input("Enter three values: ").split(" ")

In the above code based on the number of input numbers others is a list with zero to infinite numbers in it. For example, if you write 1 10 2. others is a list with [10, 2] value and if you just write one number 1, others is an empty list.

CodePudding user response:

def take3Input():
    Loop = True
    while Loop:
        try:
            n,h,y=input("Enter three values: ").split(" ")
            Loop =False
        except:
            print("please input 3 values")


take3Input()
  • Related