0

Ok my program looks like this:

U = set([0,1,2,3,4,5,6,7,8,9])
A = set([2,4,5])
B = set([5,8])
C = set([5])

def main():
    print('This is a program to give complement to given elements')
    x = input('Enter elements bla bla: ')
    y = U.difference(x)
    print('The complement of given elements are:', y)

main()

As you might have guessed, if I input e.g. 'A'. I input the string A, not the list [2,4,5] My question is:
How do I make a input where I can input A as a list and not as a string?

Ps. Sorry for any incorrect english

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by simpatar
0

You can enter a python list and then convert it using eval

userInput = input("Enter a python list: ")
mylist = eval(userInput.strip())

# check that it's a list
assert(isinstance(mylist, list))
print(mylist)

""" running the program in a shell:
$ python3 myinput.py
Enter a python list: [3, 4, 5]
[3, 4, 5]
"""
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.