Callan Callan - 1 year ago 166
Python Question

Enter list in Python3

How can I ask from the user to enter a list in Python3,so as every time the program runs,the input list is the mutable data of the program?

For example, number lists,that user maybe enters are:

L1 = [7,9,16,25]
L2 = [5,10,15,20]
L3 = [10,17,19,24]
,`L4 = [16,20,20,23]
Please write the commensurable commands/statements.

Answer Source

python3's input() now always returns a string instead of evaluating the text as it did in python2.7. this means that you have to convert an input string into the type of data you need. a simple way of achieving this is to prompt the user to enter a list of numbers separated by commas. the resulting string can then be split into a list of strings by calling the string's .split() method. after converting to a list of strings, you can call int() or float() to convert each string into an individual number.


s = input("enter a list of numbers separated by commas:") #string
l = s.split(",")  #list
n = [int(x) for x in l] #numbers
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download