-3

I'm a newbie in python & using python 3.4.3 in PyCharm in windows. Suppose that i have a list in python in which (0,2,4)th elements are names(string) and (1,3,5)th elements are theirs' rolls(int)

list = ['a',12,'b',16,'c',20]

if i want to prompt the user to enter a roll number, then delete the roll and the previous string with:

delRoll = list.index(int(input("Delete roll no: ")))
del (list[delRoll - 1: delRoll + 1])

i tested it worked fine(e.g.deleted the name and roll) if the user enters the numbers enlisted in the list , But how to find if the users hasn't entered a roll that is not enlisted in the list & give him/her a error message?

WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53

2 Answers2

3

You can use a simple if-else pair for this:

l = ['a',12,'b',16,'c',20]
roll = int(input())
if roll in l:
    del (l[l.index(roll) - 1: l.index(roll) + 1])
    print("Succes! Your list is ",l)
else:
    print("Number not in list.")

If the number one enters is 12, the result is Succes! Your list is ['b', 16, 'c', 20].

As @Rawing pointed out in the comments, this method has too much linear time complexity, and thus I recommend this method:

l = ['a',12,'b',16,'c',20]
roll = int(input())
i = 0
try:
    i = l.index(roll)
except ValueError:
    print("The given number is not in the list.")
else:
    del(l[i-1:i+1])
    print("Succes, your list is: ",l)

Quick note: You should not use list as a variable name, because it is the keyword for a list (array).

Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
  • `if roll in l` has a linear time complexity, as does `l.index(...)`. Your code has 3 occurences of linear complexity, with no good reason. Using a `try: ... except:` like in [Zed Evan's answer](http://stackoverflow.com/a/44108353/1222951) would be a better way to do this. – Aran-Fey May 22 '17 at 08:50
  • @Rawing fixed now. – Mr. Xcoder May 22 '17 at 08:59
  • @Mr.Xcoder , thanks a lot !! it worked.... i'm totally new in python, so didn't know about that `try` `except` method .... :-) One more ques.: why did you initialize `i = 0 ' first ?? – Md. Fakruddin Gazzali Fahim May 22 '17 at 09:13
  • Because if I wouldn't have initialised it, I couldn't have used it in the `else` clause: `del(l[i-1:i+1])`, because it would be declared locally in the `try` statement, which would be useless since it would be moved outside of its usage scope. Basically, I have declared it globally to be able to use it everywhere in the program after its initialization – Mr. Xcoder May 22 '17 at 09:16
2
list = ['a',4,'b',2]

try:
    b=a.index('a')
except ValueError:
    "Throw Error"
else:
    "Do something with the value"
Zed Evans
  • 126
  • 1
  • 12
  • ummm... what's b and a here in your `try` segment,please?? :-) – Md. Fakruddin Gazzali Fahim May 22 '17 at 09:02
  • we try to get an index of a value , if we do get 1 we will go to the else block, but if he does not exist , Python will throw an error , called ValueError , so we need to catch it and do something , in your case is to return it . b is a var only for checking his not infecting the code – Zed Evans May 22 '17 at 09:05