Input 2 Variables Separated By A Comma In A Single Line
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 Enter a range: then the user would input 2
Solution 1:
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.
Solution 2:
num1, num2 = raw_input('Enter a range: ').split(',')
Solution 3:
x,y = input("Enter range: ")
If you want them as numbers it's best not to use raw_input.
Solution 4:
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)
Solution 5:
In python3 you can directly use input() method instead of raw_input()
var1,var2 = input().split(',')
Post a Comment for "Input 2 Variables Separated By A Comma In A Single Line"