How to read two inputs separated by space in a single line?

前端 未结 11 518
天涯浪人
天涯浪人 2020-12-23 22:23

I want to read two input values. First value should be an integer and the second value should be a float.

I saw Read two variables in a single line with Python, but

相关标签:
11条回答
  • 2020-12-23 22:55

    In Python 2.7, I use this

    A,B = raw_input().split(" ")
    
    A = int(A)
    
    B = float(B)
    
    print(A)
    
    print(B)
    

    Output

    34 6.9

    34

    6.9

    0 讨论(0)
  • 2020-12-23 23:01

    If you wish to take as many inputs as u want then following:

        x=list(map(str,input().split())) 
        print(x)
    

    If you want two inputs:

       x,y=x,y=list(map(str,input().split()))
       print(x,y)
    
    0 讨论(0)
  • 2020-12-23 23:02

    Read 3 inputs separated by space...

    arr = input().split(" ")
    A = float(arr[0])
    B = float(arr[1])
    C = float(arr[2])
    print(A)
    print(B)
    print(C)
    
    0 讨论(0)
  • 2020-12-23 23:03

    map(str,input().split()) that is how you do it.

    0 讨论(0)
  • 2020-12-23 23:06

    Like this:

    In [20]: a,b = raw_input().split()
    12 12.2
    
    In [21]: a = int(a)
    Out[21]: 12
    
    In [22]: b = float(b)
    Out[22]: 12.2
    

    You can't do this in a one-liner (or at least not without some super duper extra hackz0r skills -- or semicolons), but python is not made for one-liners.

    0 讨论(0)
  • 2020-12-23 23:07

    Simpler one liner(but less secure):

    map(eval, raw_input().split())
    
    0 讨论(0)
提交回复
热议问题