Guttag's Finger Exercises: Enter 10 integers, find largest odd integer and print result I'm making my way through the Intro to Computation and Programming test by John Guttag and he gives these small exercises to practice gearing up into computational thinking. As of Ch.2, we've covered basic input and print commands as well as some simple iteration stuff. 

The question is as follows: 


  Write a piece of code that 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.


counter = 0
ans = 0
even=0
while counter &lt; 10:
    x=int(raw_input('Please enter an integer: '))
    if x%2!=0:          #Checks if the input is odd 
        if ans &lt; x:     #If it is, is the input larger than the held value for 'ans'?
            ans = x     #If yes, replace the value for 'ans' with the input
            counter +=1 #Move the counter up by one, ending at 10
    else:               #If it wasn't odd, add 1 to the even counter and to the integer counter, keeping ans the same
        even+=1
        counter+=1
if even == 10:          #if all numbers entered were even, this counter would be 10, and thus would give this response
    print 'All numbers were even'
else:                   #if at least 1 odd number was entered, the even counter would not be 10 and it would print the answer
    print 'the largest odd number entered was ', ans


Is there a more efficient way of writing this out? I wonder if I can word my if statements to include more parameters to cut down on the number of if/else statements? Also keep in mind this text uses python 2.7 and that I'm not super far into the book. I'm mainly trying to get the whole computational thinking thing down.
Python 2.x vs. Python 3.x

As a start, welcome to code review and since this is your first time here, well done, you did a good job here for a first post. One of the most confusing topics for beginner programmers is to choose which version of Python to learn. However there are more benefits to learn Python 3.x since it's the current version of Python and very soon there will be no future security or bug fixes for Python 2.x. and most of the developers nowadays are using Python 3.x, and as I'm familiar with the book you indicated, I know it's written in Python 2.x therefore I suggest you try to look things up and try to apply the same logic you encounter in exercises in Python 3.x (you will just be using a slightly different syntax).

Besides Python 3.x contains a bunch of new features that you'll eventually start using:


the print function
f strings
integer division
and many other features


Style

Here's a reference style guide (pep8) https://www.python.org/dev/peps/pep-0008/, the official Python guide to style you could read and here are a few comments about style:


If an assignment has a right hand side, then the equality sign should have exactly one space on both sides.

even=0

x=int(raw_input('Please enter an integer: '))

if x%2!=0:
Use inline comments sparingly.
An inline comment is a comment on the same line as a statement.
Inline comments should be separated by at least two spaces from the
statement. They should start with a # and a single space.
Use f-strings for better formatting.


ex: print(f'largest odd: {odd_num}')

The if elif else statements:

An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.

The else statement is an optional statement and there could be at most only one else statement following if.

The use of functions and lists:

You can use functions in programming to bundle a set of instructions that you want to use repeatedly or that, because of their complexity, are better self-contained in a sub-program and called when needed. That means that a function is a piece of code written to carry out a specified task. To carry out that specific task, the function might or might not need multiple inputs. When the task is carried out, the function can or can not return one or more values.

You can also use lists when you have more than a few items to do operations on.
A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside of a list is called an item. Just as strings are defined as characters between quotes, lists are defined by having values between square brackets [ ]

The code can be simplified as follows:

def get_odd_max(n):
    """Return maximum odd number from n numbers, print a message for no odds"""
    odds = []
    evens = []
    for _ in range(n):
        # the use of try and except to catch illegal types without stopping the code execution.
        try:
            number = int(input('Enter an integer: '))
            if number % 2 == 0:
                evens.append(number)
            if number % 2 != 0:
                odds.append(number)
        except ValueError:
            print('Expected int.')
    if odds:
        # f-strings for a cleaner code
        # usually a function does not print anything(it's for the sake of the example)
        print(f'Maximum odd value: {max(odds)}')
        return max(odds)
    else:
        print('No odds found.')
        return 0


if __name__ == '__main__':
    print(get_odd_max(10))