For your current program we can improve a couple things:
- Rename
odd to odds (since it is a list).
- Use
not odds instead of len(odds) == 0 (see How do I check if a list is empty? for a reason as to why this is preferred).
- Delete
counter. Since we only use counter in the while condition, we can actually replace the whole while with for _ in range(10).
- Follow PEP 8. For example, using 4 spaces for each indentation level.
Factoring in all these changes, we get:
odds = []
for _ in range(10):
x = int(input("Enter a number: "))
if abs(x) % 2 != 0:
odds.append(x)
if not odds:
print("No odd number was entered")
else:
print("The largest odd number is:", max(odds))
But we can also improve the efficiency of this program. Right now we keep track of all odd numbers, before choosing the max. This means that the space complexity is O(N). We can change this to O(1) by keeping track of the largest odd number like so:
max_odd = None
for _ in range(10):
x = int(input("Enter a number: "))
if abs(x) % 2 != 0:
max_odd = x if max_odd is None else max(max_odd, x)
if max_odd is None:
print("No odd number was entered")
else:
print("The largest odd number is: ", max_odd)
Note that we use None to signify that no odd number has been entered so far, in which case upon an odd number being entered we set max_odd to x directly. Otherwise, we set max_odd to max(max_odd, x).
For this type of program you will not notice the increase in efficiency due to reducing the space complexity. But learning to recognize where these reductions are possible will allow you to see the same patterns in programs where it does matter.