Skip to main content
Deleted a variable that wasn't been used
Source Link

I would do it something like this:

def is_odd(x):
    return x & 1 and True or False


olist = []
retry = False

for i in xrange(10):
    while True:
        try:
            n = int(raw_input('Enter a value: '))
            break
        except ValueError:
            print('It must be an integer.')
            continue

    if is_odd(n):
        olist.append(n)

try:
    print 'The largest ODD value is %d' % max(olist)
except ValueError:
    print 'No ODD values were entered'

The is_odd function is checking the last bit of the integer to see if it is set (1) or not (0). If it's set then it must be odd. Eg: 010 (2 is not odd) 011 (3 is odd).

Also, I would filter odds on the fly so I don't need to parse the list multiple times and I would use xrange (a generator) to keep memory "safe".

For this case those "optimizations" wouldn't be necessary as the list is only size 10 and there isn't much computation but if you had to take a lot of integers you would need to consider it.

I would do it something like this:

def is_odd(x):
    return x & 1 and True or False


olist = []
retry = False

for i in xrange(10):
    while True:
        try:
            n = int(raw_input('Enter a value: '))
            break
        except ValueError:
            print('It must be an integer.')
            continue

    if is_odd(n):
        olist.append(n)

try:
    print 'The largest ODD value is %d' % max(olist)
except ValueError:
    print 'No ODD values were entered'

The is_odd function is checking the last bit of the integer to see if it is set (1) or not (0). If it's set then it must be odd. Eg: 010 (2 is not odd) 011 (3 is odd).

Also, I would filter odds on the fly so I don't need to parse the list multiple times and I would use xrange (a generator) to keep memory "safe".

For this case those "optimizations" wouldn't be necessary as the list is only size 10 and there isn't much computation but if you had to take a lot of integers you would need to consider it.

I would do it something like this:

def is_odd(x):
    return x & 1 and True or False


olist = []

for i in xrange(10):
    while True:
        try:
            n = int(raw_input('Enter a value: '))
            break
        except ValueError:
            print('It must be an integer.')
            continue

    if is_odd(n):
        olist.append(n)

try:
    print 'The largest ODD value is %d' % max(olist)
except ValueError:
    print 'No ODD values were entered'

The is_odd function is checking the last bit of the integer to see if it is set (1) or not (0). If it's set then it must be odd. Eg: 010 (2 is not odd) 011 (3 is odd).

Also, I would filter odds on the fly so I don't need to parse the list multiple times and I would use xrange (a generator) to keep memory "safe".

For this case those "optimizations" wouldn't be necessary as the list is only size 10 and there isn't much computation but if you had to take a lot of integers you would need to consider it.

Source Link

I would do it something like this:

def is_odd(x):
    return x & 1 and True or False


olist = []
retry = False

for i in xrange(10):
    while True:
        try:
            n = int(raw_input('Enter a value: '))
            break
        except ValueError:
            print('It must be an integer.')
            continue

    if is_odd(n):
        olist.append(n)

try:
    print 'The largest ODD value is %d' % max(olist)
except ValueError:
    print 'No ODD values were entered'

The is_odd function is checking the last bit of the integer to see if it is set (1) or not (0). If it's set then it must be odd. Eg: 010 (2 is not odd) 011 (3 is odd).

Also, I would filter odds on the fly so I don't need to parse the list multiple times and I would use xrange (a generator) to keep memory "safe".

For this case those "optimizations" wouldn't be necessary as the list is only size 10 and there isn't much computation but if you had to take a lot of integers you would need to consider it.