lundi 15 février 2021

How to create a if condition for type INT? [duplicate]

lst = []
while True:
    arr = eval(input("Enter number of elements: "))
    if type(arr) == int:
        for i in range(0, arr):
            ele = eval(input("Enter the elements:"))
            if type(ele) == int:
                lst.append(ele)
                print(lst)
            else:
                print("Invalid Input")
    else:
        print("Invalid Input")
        break

How can I run this without a single error? Whenever I press any other button it gives an error. According to the if else condition it should print "Invalid Input" but instead it gives this error and ends

Enter number of elements: 4
Enter the elements:s
Traceback (most recent call last):
  File "C:\Users\5490 i5 10 th\pythonProject\main.py", line 220, in <module>
    ele = eval(input("Enter the elements:"))
  File "<string>", line 1, in <module>
NameError: name 's' is not defined

Process finished with exit code 1

Then I have to run the whole program again. Is there a way to avoid this? (Still Learning)

Aucun commentaire:

Enregistrer un commentaire