Skip to main content
added 1 characters in body
Source Link

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for oddness (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

(The ValueError happens when max doesn't receive any values.)

Another way of writing that would be (note the lack of square brackets):

try:
    print('The largest odd value entered was',
          max(filter(odd,
                     map(int, (input('Enter a value: ')
                               for _ in range(10))))))
except ValueError:
    print('No odd values were entered.')

where (input ...) is a generator expression.

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for oddness (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

(The ValueError happens when max doesn't receive any values.)

Another way of writing that would be (note the lack of square brackets):

try:
    print('The largest odd value entered was',
          max(filter(odd,
                     map(int, (input('Enter a value: ')
                               for _ in range(10))))))
except ValueError:
    print('No odd values were entered.')

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for oddness (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

(The ValueError happens when max doesn't receive any values.)

Another way of writing that would be (note the lack of square brackets):

try:
    print('The largest odd value entered was',
          max(filter(odd,
                     map(int, (input('Enter a value: ')
                               for _ in range(10))))))
except ValueError:
    print('No odd values were entered.')

where (input ...) is a generator expression.

added 1 characters in body
Source Link

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for trueoddness (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

(The ValueError happens when max doesn't receive any values.)

Another way of writing that would be (note the lack of square brackets):

try:
    print('The largest odd value entered was',
          max(filter(odd,
                     map(int, (input('Enter a value: ')
                               for _ in range(10))))))
except ValueError:
    print('No odd values were entered.')

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for true (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for oddness (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

(The ValueError happens when max doesn't receive any values.)

Another way of writing that would be (note the lack of square brackets):

try:
    print('The largest odd value entered was',
          max(filter(odd,
                     map(int, (input('Enter a value: ')
                               for _ in range(10))))))
except ValueError:
    print('No odd values were entered.')
added 5 characters in body
Source Link

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd is None:
    print('No'The oddlargest valuesodd werevalue entered.' was', maxOdd)
else:
    print('The largest'No odd value enteredvalues was',were maxOddentered.')

Note that I am treating non-zero values as True, so value % 2 is a test for true (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = input('Enter a value: ')
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd is None:
    print('No odd values were entered.')
else:
    print('The largest odd value entered was', maxOdd)

Note that I am treating non-zero values as True, so value % 2 is a test for true (because it gives a non-zero remainder, and Python treats non-zero values as true).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

No-one has given the code I would write, which doesn't create a list but instead tracks the answer in a loop. The logic is a little more complex, but it avoids storing values (here it doesn't really matter, since it's only 10 values, but a similar problem with input from another program, might have more data than fits in memory).

maxOdd = None
for _ in range(10):
    value = int(input('Enter a value: '))
    if (value % 2 and (maxOdd is None or value > maxOdd)):
        maxOdd = value
if maxOdd:
    print('The largest odd value entered was', maxOdd)
else:
    print('No odd values were entered.')

Note that I am treating non-zero values as True, so value % 2 is a test for true (because it gives a non-zero remainder, and Python treats non-zero values as true). Similarly, if maxOdd tests that maxOdd is not None (nor zero, but it cannot be zero, because that is even).

Another way, closer to the other answers, in that it treats the numbers as a sequence, but still avoiding storing everything in memory, is to use a generator:

from itertools import islice

def numbers():
    while True:
        yield input('Enter a value: ')

def odd(n):
    return n % 2

try:
    print('The largest odd value entered was',
          max(filter(odd, map(int, islice(numbers(), 10)))))
except ValueError:
    print('No odd values were entered.')

This is more "advanced", but numbers() creates a generator, and the chain of functions max, filter, map, islice effectively call that for each value (simplifying slightly). So the end result is a kind of pipeline that, again, avoids keeping everything in memory.

Source Link
Loading