-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)
        


        
3
  • Quite a lot of criteria! Also, what have you tried, and what isn't working? Commented Aug 15, 2018 at 2:25
  • 2
    Please show that you've tried this yourself. Otherwise it seems like you just want people to write code for you Commented Aug 15, 2018 at 2:25
  • I am new in the coding area, just curious to know the answer Commented Aug 15, 2018 at 3:11

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.