1

this might be a very simple mistake but i dont know where my mistake is. i have this code, i'm trying to remove a slot from the array as the user already choose a slot. but it doesn't work. can anyone help?

slot = ['8 AM', '9 AM', '10 AM', '11 AM' , '12 AM' , '2 PM', '3 PM', '4 PM']
print "slot available:" , slot
print 'Choose your slot'
slotchoose = raw_input ("choose:") 
if slotchoose == '8 AM' :
    slot.remove ( '8 AM ' )
else :
    if slotchoose == '9 AM' :
        slot.remove ( '9 AM' )
    else :
        if slotchoose == '10 AM' :
            slot.remove ('10 AM')
        else :
            if slotchoose == '11 AM':
                slot.remove ('11 AM')
            else:
                if slotchoose == '12 AM':
                    slot.remove ('12 AM')
                else :
                    if slotchoose == '2 PM' :
                        slot.remove ('2 PM')
                    else :
                        if slotchoose == '3 PM':
                            slot.remove ('3 PM')
                        else :
                            if slotchoose == '4 PM' :
                                slot.remove ('4 PM')

print "Slot aavail :" , slot 

here is sample of the output :

slot available: ['8 AM', '9 AM', '10 AM', '11 AM', '12 AM', '2 PM', '3 PM', '4 PM']
Choose your slot
choose:8 AM 
Slot aavail : ['8 AM', '9 AM', '10 AM', '11 AM', '12 AM', '2 PM', '3 PM', '4     PM']

the "8 AM" shouldn't be there

3
  • 2
    Do you know about elif? Commented Feb 2, 2016 at 10:21
  • If you only work with small arrays, you can put the stuff you haven't chosen in a temp array and replace the first one. And like tobias said, elif would be better than a lot of if else Commented Feb 2, 2016 at 10:22
  • 1
    Also, do you know that all this code can effectively be replaced with if slotchoose in (some valid values): slot.remove(slotchoose)? Commented Feb 2, 2016 at 10:23

2 Answers 2

2

Is indeed a simple mistake: you put an extra space.

slot.remove ( '8 AM ' )

should be:

slot.remove ( '8 AM' )
Sign up to request clarification or add additional context in comments.

1 Comment

The error would have been avoided had he used slot.remove(slotchoose). And then it would have turned out that all the if / elses were the same, and could be removed :-)
1

replace slot.remove ( '8 AM ' ) with

slot.remove ( '8 AM' )

you can make your code pythonic:

slot = ['8 AM', '9 AM', '10 AM', '11 AM' , '12 AM' , '2 PM', '3 PM', '4 PM']
print "slot available:" , slot
print 'Choose your slot'
slotchoose = raw_input ("choose:") 
if slothchoose in slot:
     slot.remove(slothchoose)   
print "Slot aavail :" , slot 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.