viththi
viththi

Reputation: 151

Need to use the while loop to input Integer only

I'm writing a simple programme to do the follow operation:

  1. Define number of elements to be added to the list (.i.e. N=6)
  2. Add the each element of the list by using input method [1,2,3,4,5,6]

But when i do the second step i have the following requirement.

I supposed to input integers always. If I add a string or other type of input, code should request to add Integer, until adding all the elements. End of the coded i suppose to add N number of Integers.

I have tried to do this with try/except as below code

def request_for_numbers():
    list2 = []
    number = int(input("Please input numbers need to be added to the list "))
    while True:
        if len(list2)<=number:
            element = input("Please input the element of the List : ")
            while True:
                try:
                    element=int(element)
                except:
                    print("Add Integer Value")
                    element=input()
            list2.append(element)
        
    print(len(list2))

When I tried this code, if i enter string value, i can see the output to request to enter the integer value. (So no problem here) But if i add the integer, it is stopped with the first iteration. (.i.e. I can't add multiple integers to the list)

Upvotes: 0

Views: 6387

Answers (3)

Adhun Thalekkara
Adhun Thalekkara

Reputation: 723

def request_for_numbers():
    list2 = []
    while True:
        number = input("Please input numbers need to be added to the list ")
        try:
            number = int(number)
            break;
        except ValueError:
            pass
    
    
    while number!=0:
        element = input("Please input the element of the List : ")
        try:
            element=int(element)
            list2.append(element)
            number-=1
        except ValueError:
            print("Please enter an Integer Value")
    
    print(len(list2))
request_for_numbers()

your while loop is not exiting

Upvotes: 1

Zircoz
Zircoz

Reputation: 512

Why do you need the second while True in your code? It creates an infinite loop of trying to parse element as Integer.

def request_for_numbers():
list2 = []
number = int(input("Please input numbers need to be added to the list "))
while True:
    if len(list2)<number:
        element = input("Please input the element of the List : ")
        #while True:
        try:
            element=int(element)
            list2.append(element)
        except ValueError:
            print("Add Integer Value")
            element=input()
    else:
        break
print(len(list2))

And try to use the exception you are expecting in except instead of catching all errors for it, at least when you know which error it will be.

Upvotes: 1

Ibukun Muyide
Ibukun Muyide

Reputation: 1298

You can re-write it as below

 def request_for_numbers():
    list2 = []
    number = int(input("Please input numbers need to be added to the list "))
    while True:
        if len(list2) > number:
            break
        element = input("Please input the element of the List : ")
        if (element.isdigit()):
            list2.append(element)
        else:
             print("Add Integer Value")
    
         
    
    print(len(list2))

Upvotes: 1

Related Questions