Two values from one input in python?

前端 未结 18 2192
小鲜肉
小鲜肉 2020-11-28 03:46

This is somewhat of a simple question and I hate to ask it here, but I can\'t seem the find the answer anywhere else: is it possible to get multiple values from the user in

18条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 04:15

    In Python 2.*, input lets the user enter any expression, e.g. a tuple:

    >>> a, b = input('Two numbers please (with a comma in between): ')
    Two numbers please (with a comma in between): 23, 45
    >>> print a, b
    23 45
    

    In Python 3.*, input is like 2.*'s raw_input, returning you a string that's just what the user typed (rather than evaling it as 2.* used to do on input), so you'll have to .split, and/or eval, &c but you'll also be MUCH more in control of the whole thing.

提交回复
热议问题