patternpythonMinor
Find the largest odd number (follow-up code)
Viewed 0 times
numberthelargestfollowfindcodeodd
Problem
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.
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.'Solution
Be careful of changing back and forth from
Also, you don't need to keep the list of
And this way, you don't even need to build a list. You can instead pass something called a generator expression directly to
However, this would raise a
' 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."Code Snippets
print "You'll be asked to enter 10 integers."
print "The program will look for odd numbers, if any."max_value = max([item for item in numbers_entered if item % 2 == 1])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."Context
StackExchange Code Review Q#105887, answer score: 4
Revisions (0)
No revisions yet.