Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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."
share|improve this question
1  
In what language is this written? Can you describe what the intention of this code is? –  skiwi Jul 18 at 18:53
    
yah sorry python and it asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect. –  Jared Jul 18 at 18:54
3  
I suggest editing your post to include the intent of the program at the top. –  raptortech97 Jul 18 at 18:56
    
it only allows me to enter 150 characters and i cant fit that i tried it before and it told me it was too long –  Jared Jul 18 at 18:57
2  
Not in the title! In the post itself ;) try coming up with a short witty title, you'll get more eyes on ;) –  Mat's Mug Jul 18 at 18:58

2 Answers 2

up vote 4 down vote accepted

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"

How to read this line:

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.

share|improve this answer
    
input is a built-in function both in Python 2.x and 3.x, you should not use it as a variable name. –  Jaime Jul 18 at 20:08
    
Also, you could simply keep track of the largest odd number, no need to store all odds. –  Jaime Jul 18 at 20:10
    
More Explanation for the OP please –  Malachi Jul 18 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. –  mleyfman Jul 18 at 20:22
1  
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 –  jcollado Jul 18 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.

share|improve this answer
    
List comprehensions is where python begins to show its magic –  matheussilvapb Jul 22 at 22:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.