1
seatList1 = {'seat1': 1, 'seat2': 2, 'seat3': 3, 'seat4': 4, 'seat5': 5, 'seat6': 6, 'seat7': 7, 'seat8': 8, 'seat9': 9, 'seat10': 10 }   

We were asking to make a ticket selling system, there are ten seats in total. I am planning to check they are ordered or not by data types. Once user typed their details in and choose an empty seat, the value of seat they choose will become to their first name. So that, to check a seat is ordered or not, the only thing I need to do is check those value of keys are integer or string

For example: user typed his details first, and then choose seat1, after that the value of 'seat1' key will be his name, instead of

'seat1': 1

But

'seat1': 'Tom'

If the value is a String instead of a number, there's no doubt that this seat has been ordered. But I don't know how to do that. I can use "if" to solve this problem, but that is too inefficient. Is there a better way to solve it? Thank you very much!

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • 1
    `if isinstance(seatList1['seat1'], str):` – furas Jan 03 '20 at 04:08
  • 2
    Possible duplicate: https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python – sshashank124 Jan 03 '20 at 04:10
  • 1
    Do you need the values as numbers? I would suggest to have it as None then you can check if it is reserved with: `if not seatList1["seat1"]: "not reserved" else: "reserved"` – Boendal Jan 03 '20 at 04:12
  • did you test how "inefficient" is it? Probably it will faster then you need. – furas Jan 03 '20 at 04:17
  • This does not seem like an efficient method of dealing with your ticket selling system. This does look like an assignment for a class/course so my question is have you learnt how to make classes in python ? – Karan Shishoo Jan 03 '20 at 04:19
  • Please share example code of what you tried, what happened and what did you expect to happen? Why would an `if` be too "inefficient"? In what context? – Grismar Jan 03 '20 at 04:33

1 Answers1

1

You should build your system using get

Check if seat is taken or assign it

seatList1['seat1'] = seatList1.get('seat1', 'user_name')

Kenan
  • 13,156
  • 8
  • 43
  • 50