Find the largest odd number (follow-up code)

Ask user to input 10 integers and then print the largest odd number that was entered. If no odd number was entered, print a message to that effect.

This is a follow-up code on feedback from Find the largest odd number

This time I've taken care of a bug related to the number of integers entered and tried to handle invalid input if any. Please let me know if the code still can be tidied up in any way. I'm a beginner programmer.

print "You'll be asked to enter 10 integers."
print 'The program will look for odd numbers, if any.'
print 'Then, it will print the largest odd number.'
print 'Press Enter to start.'
raw_input('-> ')

numbers_entered = []

while True:
try:
number = int(raw_input('Enter an integer: '))
numbers_entered.append(number)
except ValueError:
print 'That was not an integer.'

if len(numbers_entered) == 10:
break

odd_numbers = [item for item in numbers_entered if item % 2 == 1]

if odd_numbers:
print 'The largest odd number entered was {}.'.format(max(odd_numbers))
else:
print 'No odd number was entered.'

• You can move the break condition into the while statement: while len(numbers_entered) <= 10). Commented Sep 28, 2015 at 6:14
• You could check if number is odd and if it's the greatest one so far in the while statement. Just an alternative to your way of doing it, slightly more efficient memory-wise. Commented Sep 28, 2015 at 7:33
• @mostruash, please look at codereview.stackexchange.com/questions/105821/… That's my previous post on the same problem.
– srig
Commented Sep 28, 2015 at 7:55

Be careful of changing back and forth from ' and ". I can see that you probably used the double quotes just so you could include an apostrophe, but I think it's best to stay consistent and use them throughout since you never need to print any "'s later in the program.

print "You'll be asked to enter 10 integers."
print "The program will look for odd numbers, if any."


Also, you don't need to keep the list of odd_numbers if all you want is the highest one. So you could just directly get the max value:

max_value = max([item for item in numbers_entered if item % 2 == 1])


And this way, you don't even need to build a list. You can instead pass something called a generator expression directly to max. It's like a list comprehension, but it doesn't create a full list. It can be used in both for loops and functions that lake lists, like max does.

However, this would raise a ValueError if you had no odd numbers in the list, as max can't take an empty sequence. But you could just use this as your test instead of if odd_numbers.

try:
max_value = max(item for item in numbers_entered if item % 2 == 1)
print "The largest odd number entered was {}.".format(max_value)
except ValueError:
print "No odd number was entered."


Firstly, seeing as you want to loop until the length of "numbers_entered" is 10, I would change:

while True:


to

while len(numbers_entered) < 10:


so that you don't have to include that if statement.

Also, in the same way that you make sure that only integers are added, you can also make sure that only odd numbers are added. Add the following code into your try statement between "number = int(raw_input('Enter an integer: '))" and "numbers_entered.append(number)"

if number % 2 == 0:
raise


You might then want to change your exception from

except ValueError:
print 'That was not an integer.'


to just

except:
print 'That was not an odd integer'


This way the exception will be raised if the inputted value is either not an integer or it's odd. This would be your finished code.

print "You'll be asked to enter 10 integers."
print 'The program will look for odd numbers, if any.'
print 'Then, it will print the largest odd number.'
print 'Press Enter to start.'
raw_input('-> ')

numbers_entered = []

while len(numbers_entered) < 10:
try:
number = int(raw_input('Enter an integer: '))
if number % 2 == 0:
raise
numbers_entered.append(number)
except:
print 'That was not an odd integer.'

odd_numbers = [item for item in numbers_entered if item % 2 == 1]
print 'The largest odd number entered was {}.'.format(max(odd_numbers))

• Welcome to Code Review! Good note on the while loop. Though I think that the OP wants to consider an even number as valid input and later filter it out. ie. The brief says 10 integers, not specifically 10 odd integers. Commented Sep 28, 2015 at 9:31
• You really shouldn't be using bare except statements unless you have a good reason to not specify the exception types. Commented Sep 28, 2015 at 11:55
• I disagree, I think you should use bare except statements unless you have a good reason to specify the exception types, there is no such reason in this application. Commented Sep 28, 2015 at 14:45
• The reason is, what if an unexpected exception occurs? Your code will happily catch it and behave wrongly. If an exception occurs that your code's not able to handle, it should let the exception on by in hopes that the caller is able to handle it. Commented Sep 28, 2015 at 16:02
• In what way could there be an incorrect exception in this case? The only way it could give an exception is if the inputted value is either odd or not an integer Commented Sep 28, 2015 at 17:34

You can do this without storing the inputs and looping through them afterwards. You only need to remember whether or not an odd number was entered and which one is the largest. My Python is weak so I'll use pseudocode:

var hasUserEnteredOddNumber = false
var largestOddNumber = 0

for(var i = 0; i < 10; i++)
{
if(input mod 2 == 1)
{
if(hasUserEnteredOddNumber)
largestOddNumber = max(largestOddNumber, input)
else
largestOddNumber = input
hasUserEnteredOddNumber = true
}
}

if(hasUserEnteredOddNumber)
Print("The largest odd number was " & largestOddNumber)
else
Print("No odd number was entered")


You'll need a ReadIntFromConsole function. It'll be something like this:

function int ReadIntFromConsole()
{
while(true)
{