# Printing inputted, largest, odd integer

The program asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it prints a message to that effect.

q = int(raw_input("Please enter an integer:"))
r = int(raw_input("Please enter another integer:"))
s = int(raw_input("Please enter another integer:"))
t = int(raw_input("Please enter another integer:"))
u = int(raw_input("Please enter another integer:"))
v = int(raw_input("Please enter another integer:"))
w = int(raw_input("Please enter another integer:"))
x = int(raw_input("Please enter another integer:"))
y = int(raw_input("Please enter another integer:"))
z = int(raw_input("Please enter one last integer:"))

odd = []
if q%2 != 0:
odd += [q]
if r%2 != 0:
odd += [r]
if s%2 != 0:
odd += [s]
if t%2 != 0:
odd += [t]
if u%2 != 0:
odd += [u]
if v%2 != 0:
odd += [v]
if w%2 != 0:
odd += [w]
if x%2 != 0:
odd += [x]
if y%2 != 0:
odd += [y]
if z%2 != 0:
odd += [z]
if q%2 == 0 and r%2 == 0 and s%2 == 0 and t%2 == 0 and u%2 == 0 and v%2 == 0 and w%2 == 0 and x%2 == 0 and y%2 == 0 and z%2 == 0:
print "None of the values given are odd."
else:
print max(odd), "is the largest odd number."


Use loops!

odd_nums = []
for i in xrange(10):
value = int(raw_input('Enter an integer: '))
if value % 2 != 0:
odd_nums.append(value)

if len(odd_nums) != 0:
print max(odd_nums)
else:
print "No odd values"


for i in xrange(10):


This line means starting at 0 (the default starting point for xrange()), and ending at 10-1 (upper bound is not included), execute the following code with i set to the current iteration of the loop. This executes the code exactly 10 times (i = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

The rest was made as similar to your code as possible, and so should be understandable.

• input is a built-in function both in Python 2.x and 3.x, you should not use it as a variable name. Jul 18, 2014 at 20:08
• Also, you could simply keep track of the largest odd number, no need to store all odds. Jul 18, 2014 at 20:10
• More Explanation for the OP please
– Malachi
Jul 18, 2014 at 20:11
• Added explanation for loop structure. The reason I stored all the odds is to replicate as much of his code structure as possible. Jul 18, 2014 at 20:22
• There are a few things that could be improved: xrange(0, 10) -> xrange(10), value % 2 != 0 -> value % 2, len(odd_nums) != 0 -> odd_nums, odd_nums += [value] -> odd_nums.append(value), length -> len Jul 18, 2014 at 23:36

Here is my solution:

number_count = 10

# Get number_count numbers
numbers = (int(raw_input('Number: ')) for _ in xrange(number_count))

# Filter out odd numbers
odd_numbers = [number for number in numbers if number % 2]

if odd_numbers:
print '{} is the largest odd number'.format(max(odd_numbers))
else:
print 'None of the given values is odd'


A couple of comments related to the other solutions:

• Use list comprehensions and/or generator expressions where possible instead of loops
• There's no need to use len to check for an empty list. An empty list is a evaluated as False.

If you want to use a super compact syntax, you can use an if expression for the print statement:

print ('{} is the largest odd number'.format(max(odd_numbers))
if odd_numbers
else 'None of the given values is odd')


However, I don't think this is is very readable.

• List comprehensions is where python begins to show its magic Jul 22, 2014 at 22:44

Normally, we don't want to repeat a certain line (or lines) a number of times. It makes the code long, and unreadable. Also storing the numbers in variables isn't so good because we don't want to create 100 variables to deal with 100 numbers (or even much more).

The Solution: We use loops and lists.

We use a loop that runs 10 times, each time we get an input from the user, and if it's an odd number we store it in a list. Then we print the max of the list.

===

The code in python 3:

integers = 10
#You choose it!
odds = []
#Creating a list to store the odd numbers
for i in range(integers):
#A loop that runs 10 times
num = int(input("Enter an integer: "))
#Taking an int from the user
if num % 2 != 0:
#If the number is odd
odds.append(num)