Two values from one input in python?

前端 未结 18 2070
小鲜肉
小鲜肉 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:11
    a,b=[int(x) for x in input().split()]
    print(a,b)
    

    Output

    10 8

    10 8

    0 讨论(0)
  • 2020-11-28 04:12

    You have to use the split() method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.

    For example, You give the input 23 24 25. You expect 3 different inputs like

    num1 = 23
    num2 = 24
    num3 = 25
    

    So in Python, You can do

    num1,num2,num3 = input().split(" ")
    
    0 讨论(0)
  • 2020-11-28 04:15

    The Python way to map

    printf("Enter two numbers here: ");
    scanf("%d %d", &var1, &var2)
    

    would be

    var1, var2 = raw_input("Enter two numbers here: ").split()
    

    Note that we don't have to explicitly specify split(' ') because split() uses any whitespace characters as delimiter as default. That means if we simply called split() then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,

    Python has dynamic typing so there is no need to specify %d. However, if you ran the above then var1 and var2 would be both Strings. You can convert them to int using another line

    var1, var2 = [int(var1), int(var2)]
    

    Or you could use list comprehension

    var1, var2 = [int(x) for x in [var1, var2]]
    

    To sum it up, you could have done the whole thing with this one-liner:

    # Python 3
    var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
    
    # Python 2
    var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
    
    0 讨论(0)
  • 2020-11-28 04:15

    You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):

    raw_answer = raw_input()
    answers = raw_answer.split(' ') # list of 'answers'
    

    So you could rewrite your try to:

    var1, var2 = raw_input("enter two numbers:").split(' ')
    

    Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).

    Also be aware that var1 and var2 will still be strings with this method when not cast to int.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 04:16

    You can do this way

    a, b = map(int, input().split())
    

    OR

    a, b = input().split()
    print(int(a))
    

    OR

    MyList = list(map(int, input().split()))
    a = MyList[0]
    b = MyList[1]
    
    0 讨论(0)
提交回复
热议问题