Write a program in python that reads the elements of a set from the keyboard, stores...

60.1K

Verified Solution

Question

Programming

Write a program in python that reads the elements of a set fromthe keyboard, stores them in a set, and then determines itspowerset. Specifically, the program should repeatedly ask theuser:

Enter one more element ? [Y/N]

If the user answers Y then an new element is read from thekeyboard:

Enter the new element in the set:

This cycle continues until the user answers N to the firstquestion. At that point the program shall compute the powerset ofthe set of elements provided by the user, and print it to thescreen. The powerset should not only be printed to the screen, butalso stored in an instance of set.

Important: for what stated above, the elements of the powersetcannot be sets themselves. It is ok to store them in tuples. Notethat while you can use a list to create a set (like in the examplewhere we used list of chars), you cannot store lists in a setbecause of the same reason you cannot store sets inside a set. Forexample, the following code will give you an error

>>> A = [1,2] >>> B = [3,4] >>> C =[A,B] >>> D = set(C) 3---------------------------------------------------------------------------

TypeError Traceback (most recent call last) in ----> 1 D =set(C)

TypeError: unhashable type: ’list’

The reason is that D = set(C) tries to build a set of elementsfrom the list C, and the elements of C are the lists A and B. Onthe contrary, E = set(A) would be ok, because it builds a set fromthe elements of A, and the elements of A are integers.

Answer & Explanation Solved by verified expert
3.9 Ratings (632 Votes)
Code input sets setwhileTrue option inputEnter one    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students