-3

This code is for asserting values into the list until the user inputs a negative number.

Is there a way better to implement it?

Below is my tried version.

i = -1
while i > -1:
    x = raw_input("Enter array limit")
    for i in limit(x):
        listArr = list()
        y = raw_input("Enter number")
        y.append(listArr)
        print (listArr)
        


        
Avinash Singh
  • 122
  • 2
  • 9

1 Answers1

1

Something like this should meet the requirements:

odds = []                     # similiar to `listArr` but we only want odd numbers

limit = 5

for _ in range(limit):        # Input 5 numbers into an array
    y = int(input('Enter number: '))        
    if y > -1 and y % 2 !=0:  # if odd number
        odds.append(y)        # add to array
    else:
        break                 # break after a negative number as input 

if odds:
    print(min(odds))          # and display minimum odd number 
                              # and if no negative integer input then also display minimum odd number 
else:
    print(0)                  # and if no odd number display zero

If Python2 use raw_input() as used in question code, otherwise use input().