Skip to main content
deleted 44 characters in body
Source Link
Mario Ishac
  • 977
  • 6
  • 15
x_is_valid = False

while not x_is_validTrue:
    try:
        x = int(input("Enter a number: "))
        x_is_valid = Truebreak
    except ValueError:
        continue
x_is_valid = False

while not x_is_valid:
    try:
        x = int(input("Enter a number: "))
        x_is_valid = True
    except ValueError:
        continue
while True:
    try:
        x = int(input("Enter a number: "))
        break
    except ValueError:
        continue
added 782 characters in body
Source Link
Mario Ishac
  • 977
  • 6
  • 15

There is finally one more thing you can do. If you want to allow the program to keep accumulating numbers in the event that a str is accidentally typed which cannot be parsed as a number (such as ""), we can use a try / except wrapped in a while like so:

x_is_valid = False

while not x_is_valid:
    try:
        x = int(input("Enter a number: "))
        x_is_valid = True
    except ValueError:
        continue

This would replace:

x = int(input("Enter a number: "))

in the original code. This would keep prompting the user to type a str that is parsable as an int until they do. Since this is all happening in the same iteration of the for, the count of numbers they get to type (10 in our case) would not be reduced.

There is finally one more thing you can do. If you want to allow the program to keep accumulating numbers in the event that a str is accidentally typed which cannot be parsed as a number (such as ""), we can use a try / except wrapped in a while like so:

x_is_valid = False

while not x_is_valid:
    try:
        x = int(input("Enter a number: "))
        x_is_valid = True
    except ValueError:
        continue

This would replace:

x = int(input("Enter a number: "))

in the original code. This would keep prompting the user to type a str that is parsable as an int until they do. Since this is all happening in the same iteration of the for, the count of numbers they get to type (10 in our case) would not be reduced.

Source Link
Mario Ishac
  • 977
  • 6
  • 15

For your current program we can improve a couple things:

  1. Rename odd to odds (since it is a list).
  2. 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).
  3. Delete counter. Since we only use counter in the while condition, we can actually replace the whole while with for _ in range(10).
  4. 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.