+ 1
Getting list as user_input
I started learning python a few months ago. Recently, I am trying to write a program that will enable me solve Correlation coefficient on the go but am stuck somehow in getting the user values of X and Y respectively. Is there anyway I can make user input(integer x and y) to be in the form of a list either by asking for an arrayof int separated by commas? Please fellow python programmer, I need your contribution
2 ответов
+ 3
You can get it along the routes of having the user enter multiple values on the same line but the entries will be separated with spaces. Then you can have a list comprehension that'll be in the form of a one-liner for loop that'll iterate over the list and convert each element from a string to an integer...
aList = input('Enter values separated by space:').split()
aList = [int(i) for i in aList]
print(aList)
+ 2
This can be separated by what you want, you just have to pass it to the split() method:
aList = input("Enter values separated by comma: ').split(",")