6
\$\begingroup\$

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."
\$\endgroup\$
0

3 Answers 3

4
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ input is a built-in function both in Python 2.x and 3.x, you should not use it as a variable name. \$\endgroup\$
    – Jaime
    Jul 18, 2014 at 20:08
  • \$\begingroup\$ Also, you could simply keep track of the largest odd number, no need to store all odds. \$\endgroup\$
    – Jaime
    Jul 18, 2014 at 20:10
  • \$\begingroup\$ More Explanation for the OP please \$\endgroup\$
    – Malachi
    Jul 18, 2014 at 20:11
  • \$\begingroup\$ Added explanation for loop structure. The reason I stored all the odds is to replicate as much of his code structure as possible. \$\endgroup\$
    – mleyfman
    Jul 18, 2014 at 20:22
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – jcollado
    Jul 18, 2014 at 23:36
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ List comprehensions is where python begins to show its magic \$\endgroup\$ Jul 22, 2014 at 22:44
2
\$\begingroup\$

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)
    #Add it to the list

if len(odds) == 0:
    print("You didn't enter any odd numbers!")
else:
    print("The max is: " + str(max(odds)))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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