patternpythonMinor
Find the largest odd number
Viewed 0 times
numberthelargestfindodd
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.
The above exercise is from chapter 2 of Computation and Programming Using Python by John Guttag. So far, the book has covered variable assignment, print function, conditional branching, and loops. The book is about problem solving and uses Python only as a tool to solve problems and it is meant to be used with Python 2.x. I need to know if my solution to the exercise can be "straightened up" in any way.
The above exercise is from chapter 2 of Computation and Programming Using Python by John Guttag. So far, the book has covered variable assignment, print function, conditional branching, and loops. The book is about problem solving and uses Python only as a tool to solve problems and it is meant to be used with Python 2.x. I need to know if my solution to the exercise can be "straightened up" in any way.
numbers_entered = []
for _ in range(10):
number = raw_input('Enter an integer: ')
if number.isdigit():
number = int(number)
numbers_entered.append(number)
else:
print 'That was not an integer!'
odd_numbers = []
for item in numbers_entered:
if item % 2 != 0:
odd_numbers.append(item)
if odd_numbers:
print 'The largest odd number entered was %s.' % str(max(
odd_numbers))
else:
print 'No odd number was entered.'Solution
Bug
At the end of the first loop,
how many numbers will be in
10? Hopefully.
In reality: 10 minus invalid entries,
and I'm not sure that was your intention.
This way the invalid entries will be skipped, and there will always be 10 entries in list:
Handling invalid input
The user input should be valid,
invalid inputs should be the exception.
So you can simplify the handling of invalid user inputs using exceptions:
See also the glossary, and search for the word "forgiveness" in it.
Using list comprehensions
List comprehensions are awesome.
The loops could be replaced by list comprehensions
(with the help functions), for example:
Prefer
Instead of formatting with
the new preferred way is using
Notice that there's no need to call
Readability
This statement would have been better on a single line:
At the end of the first loop,
how many numbers will be in
numbers_entered?10? Hopefully.
In reality: 10 minus invalid entries,
and I'm not sure that was your intention.
This way the invalid entries will be skipped, and there will always be 10 entries in list:
while True:
number = raw_input('Enter an integer: ')
if number.isdigit():
number = int(number)
numbers_entered.append(number)
break
else:
print 'That was not an integer!'Handling invalid input
The user input should be valid,
invalid inputs should be the exception.
So you can simplify the handling of invalid user inputs using exceptions:
try:
number = int(raw_input('Enter an integer: '))
numbers_entered.append(number)
except ValueError:
print 'That was not an integer!'See also the glossary, and search for the word "forgiveness" in it.
Using list comprehensions
List comprehensions are awesome.
The loops could be replaced by list comprehensions
(with the help functions), for example:
def read_int():
while True:
try:
return int(raw_input('Enter an integer: '))
except ValueError:
print 'That was not an integer!'
numbers = [read_int() for _ in range(10)]
odd_numbers = [item for item in numbers if item % 2]
if odd_numbers:
print 'The largest odd number entered was {}.'.format(max(odd_numbers))
else:
print 'No odd number was entered.'Prefer
.format(...) for formattingInstead of formatting with
%,the new preferred way is using
.format(...):print 'The largest odd number entered was {}.'.format(max(odd_numbers))Notice that there's no need to call
str(...) when using this style.Readability
This statement would have been better on a single line:
print 'The largest odd number entered was %s.' % str(max(
odd_numbers))Code Snippets
while True:
number = raw_input('Enter an integer: ')
if number.isdigit():
number = int(number)
numbers_entered.append(number)
break
else:
print 'That was not an integer!'try:
number = int(raw_input('Enter an integer: '))
numbers_entered.append(number)
except ValueError:
print 'That was not an integer!'def read_int():
while True:
try:
return int(raw_input('Enter an integer: '))
except ValueError:
print 'That was not an integer!'
numbers = [read_int() for _ in range(10)]
odd_numbers = [item for item in numbers if item % 2]
if odd_numbers:
print 'The largest odd number entered was {}.'.format(max(odd_numbers))
else:
print 'No odd number was entered.'print 'The largest odd number entered was {}.'.format(max(odd_numbers))print 'The largest odd number entered was %s.' % str(max(
odd_numbers))Context
StackExchange Code Review Q#105853, answer score: 8
Revisions (0)
No revisions yet.