Home > Software engineering >  python program troubleshoot
python program troubleshoot

Time:12-03

if the user enters a char it should show the wrong input and continue asking for input until it reaches the range of 10 elements. how to solve this? output

list = []
even = 0

for x in range(10):
    number = int(input("Enter a number: "))
    list.append(number) 




for y in list:
    if y % 2 == 0:
        even  =1
    
print("Number of even numbers: " ,even)

for y in list:
   if y % 2 == 0:
       count = list.index(y)
       print("Index [",count,"]: ",y)

CodePudding user response:

myList = []
while len(myList) < 10:
    try:
        number = int(input("Enter a number: "))
        myList.append(number)
    except ValueError:
        print('Wrong value. Please enter a number.')
print(myList)

CodePudding user response:

Hope code is self explanatory:

arr = []
even = 0
error_flag = False

for x in range(10):
    entry = input("Enter a number: ")
    if not entry.isdigit():
        print("Entry is not a number")
        error_flag = True
        break
    arr.append(int(entry))

if not error_flag:
    brr = []
    for id, y in enumerate(arr):
        if y%2 == 0:
            brr.append([id,y])

    print(f"Even numbers are: {len(brr)}")
    for z in brr:
        print(f"Index{z[0]} is {z[1]}")


CodePudding user response:

list = []
even_list=[]
c=0

for x in range(10):
    number = (input("Enter a number: "))
    list.append(number)
    if number.isdigit()==False :
        print("wrong input")
        break 
    elif int(number)%2==0:
        even_list.append(number) 

if len(list)==10:
    print("Number of even numbers: ",len(even_list))
    for i in list:
        i=int(i)
        if (i) %2==0:
            print("Index %d : %d" %(c,i))       # print("Index",c,":",i)
        c=c 1
  • Related