0

I'm trying to change the entry of the set to an integer

I've tried isolating the entry and using the "int" function on it

contestant = input("Choose a door from the three doors:")
doors = {1,2,3}
prize = random.randint(1,3)
revealable_doors = doors - {int(contestant), int(prize)}
host = int(revealable_doors)

I expect that host will be an integer but I keep getting this error

host = int(revealable_doors)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'
  • 3
    a set with only one element is still a set... – thebjorn Apr 19 '19 at 20:43
  • 1
    Do `host = revealable_doors.pop()` instead. – blhsing Apr 19 '19 at 20:45
  • 1
    Possible duplicate of [How to extract the member from single-member set in python?](https://stackoverflow.com/questions/1619514/how-to-extract-the-member-from-single-member-set-in-python) Use one of the methods indicated and then convert to `int`. – iz_ Apr 19 '19 at 20:51
  • note also that `revealable_doors` will sometimes be a set with only one element, and will sometimes be a set with two elements – Hamms Apr 19 '19 at 21:13

1 Answers1

0

revealable_doors = doors - {int(contestant), int(prize)} will return set of one or two elements.

So you cannot just convert set to int as you did in this : host = int(revealable_doors)

You can try following code:

import random
contestant = input("Choose a door from the three doors:")
doors = {1,2,3}
prize = random.randint(1,3)
revealable_doors = doors - {contestant, prize}
host = revealable_doors.pop()
print(host)
Rushabh Sudame
  • 414
  • 1
  • 6
  • 22