input 2 variables separated by a comma in a single line

后端 未结 5 1335
长发绾君心
长发绾君心 2021-01-19 15:11

Is it possible to input 2 numbers int or float separated by a comma in a single line?

Say after the program runs it would ask the user to <

相关标签:
5条回答
  • 2021-01-19 15:48
    num1,num2 = map(float, raw_input('Enter a range: ').split(','))
    

    Alternatively, if you want to allow commas in the second value, use partition instead of split:

    s1,_,s2 = raw_input('Enter a range: ').partition(',')
    

    In this case, you'll have to convert the two strings to numbers by yourself.

    0 讨论(0)
  • 2021-01-19 15:49

    num1, num2 = raw_input('Enter a range: ').split(',')

    0 讨论(0)
  • 2021-01-19 15:54
         x,y = input("Enter range: ")
    

    If you want them as numbers it's best not to use raw_input.

    0 讨论(0)
  • 2021-01-19 15:54

    In python3 you can directly use input() method instead of raw_input()

    var1,var2 = input().split(',')
    
    0 讨论(0)
  • 2021-01-19 15:58

    It's my understanding that ast.literal_eval is safe:

    >>> x, y = ast.literal_eval(raw_input('Enter a range: '))
    Enter a range: 5, 6
    >>> x, y
    (5, 6)
    
    0 讨论(0)
提交回复
热议问题