Two values from one input in python?

前端 未结 18 2069
小鲜肉
小鲜肉 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:03

    For n number of inputs declare the variable as an empty list and use the same syntax to proceed:

    >>> x=input('Enter value of a and b').split(",")
    Enter value of a and b
    1,2,3,4
    >>> x
    ['1', '2', '3', '4']
    
    0 讨论(0)
  • 2020-11-28 04:03

    I tried this in Python 3 , seems to work fine .

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

    Input : 3 44

    Output :

    3

    44

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

    Check this handy function:

    def gets(*types):
        return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
    
    # usage:
    a, b, c = gets(int, float, str)
    
    0 讨论(0)
  • 2020-11-28 04:07

    All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.

    input_string = raw_input("Enter 2 numbers here: ")
    a, b = split_string_into_numbers(input_string)
    do_stuff(a, b)
    
    0 讨论(0)
  • 2020-11-28 04:10

    if we want to two inputs in a single line so, the code is are as follows enter code here

    x,y=input().split(" ")
    
    print(x,y)
    

    after giving value as input we have to give spaces between them because of split(" ") function or method.

    but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows int(x),int(y)

    we can do this also with the help of list in python.enter code here

    list1=list(map(int,input().split()))
    print(list1)
    

    this list gives the elements in int type

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

    Two inputs separated by space:

    x,y=input().split()
    
    0 讨论(0)
提交回复
热议问题