-1

In the code below, when we run m.choose_slot(), and enter the slot by entering the number, it gets popped from the entered location instead of the number from the list itself ,even though in the for loop I mentioned i==b (iterator is equal to the entered number that is b). I want the number entered to be popped.P.S I am a beginner in Python and I am doing this as a part of my first project.

class Doctor():
    a=[0,1,2,3,4,5,6,7,8]
    def __init__(self,hours):
        self.hours=hours
        print("The doctor is Free for {} hours a day".format(self.hours))

    def scheduler(self):
        print("The available slots are:")
        for i in self.a:
            print(i)

d=Doctor(8)
d.scheduler()

class Patient(Doctor):
    def __init__(self):
        d.scheduler()

    def choose_slot(self):
        while True:
            try:
                b=int(input("Choose a slot from the given table: "))
                if b in range(1,8):
                    for i in self.a:
                        if i==b:
                            self.a.pop(b)
                            print("The slot is scheduled at {}".format(b))
                            return d.scheduler()
                            break
                    else:
                        return "The entered slot is not available!"
                        continue
            except:
                print("Please check your entry again")

m=Patient()
m.choose_slot()
stovfl
  • 14,998
  • 7
  • 24
  • 51
Hrishikesh D Kakkad
  • 171
  • 1
  • 1
  • 12
  • 1
    Please correct the indentations, you're creating instance of a class inside itself. – Mazdak Jan 05 '19 at 09:29
  • [Edit] your Question and explain in detail **why** do you inherit `Patient` from `Doctor`: `class Patient(Doctor):` – stovfl Jan 05 '19 at 09:59
  • @Kasrâmvd sure thanks for pointing it out. I didn't really notice it when copying the code from the notebook to stackoverflow. – Hrishikesh D Kakkad Jan 05 '19 at 11:00
  • @stovfl this is actually a part of a bigger project that I have just started to work on. I'll later have to inherit more capabilities of Doctor to patient. Right now it's just inheriting the scheduler(). – Hrishikesh D Kakkad Jan 05 '19 at 11:08
  • *" have to inherit more capabilities of Doctor to patient"*: This makes no sense. A `class Patient` never need *capabilities* of `class Doctor`. – stovfl Jan 05 '19 at 13:18

1 Answers1

0

The list.pop(i) removes i-th element of the list by it's index. If you need to remove element by value, use list.remove(x) method instead.

>>> a = [10, 11, 12, 12]
>>> a.pop(1)
11
>>> a
[10, 12, 12]
>>> a.remove(12)
>>> a
[10, 12]

Some links:

https://docs.python.org/3/tutorial/datastructures.html

Is there a simple way to delete a list element by value?

savfod
  • 549
  • 6
  • 9