Home > OS >  unable to take input as a tuple of integers( with one line of code)
unable to take input as a tuple of integers( with one line of code)

Time:01-20

i want the user to input in form a,b and take that as (a,b) where a,b are integers

I tried

c = tuple(int((input("enter tup").split(",")))

i understand why this is a error.The only way i am able to do this is

 c = (input("enter tup").split(",")
 c = [int(x) for x in c]
 c = tuple(c)

CodePudding user response:

.split() creates a list, and the intermediate list being created on the second line isn't necessary. Since you said you were looking for a one-liner specifically, you can do the following:

c = tuple(int(x) for x in (input("enter tup").split(",")))
print(c)

CodePudding user response:

Alternate:

c = tuple(map(int, input("enter tup").split(",")))
print(c)

CodePudding user response:

Here is your one liner code :

c = tuple(map(int ,input("enter tuple").split()))
  •  Tags:  
  • Related