Home > Blockchain >  How to declare variables from an input() with different class ( int and str)
How to declare variables from an input() with different class ( int and str)

Time:11-02

iLig1, iCol1 , iLig2 , iCol2 , carac = map(int, input ().split ())

hello I try to get this ligne of input in my code right, I get 5 variables from an input that look like this:

1 12 7 14 u

how can I declare the last one as a str properly, I tried to consider them all as str and convert the 4 first as int but I cannot interpret str as int (you know..)

Thank you for your help !

CodePudding user response:

You can use the Iterable Unpacking : (*).

# we suppose input is -> 1 2 3 4 u
>>> (*i, j) = input().split()

>>> print(i)
['1', '2', '3', '4']

>>> print(j)
u

>>> list(map(int, i))
[1, 2, 3, 4]

CodePudding user response:

This should do the job:

iLig1, iCol1 , iLig2 , iCol2 , carac = [int(x) if i<4 else x for i,x in enumerate(input ().split ())]

CodePudding user response:

One liner that converts the first 4 to int and the last to str

iLig1, iCol1 , iLig2 , iCol2 , carac = map(lambda f: f[1](f[0]), zip(input().split(), [int, int, int, int, str]))
  • Related