Skip to main content
3 of 4
added 2 characters in body
GZ0
  • 2.4k
  • 8
  • 19

Adding to the previous review:

  • When x is an integer, abs(x) % 2 is equivalent to x % 2 in Python. The output of the modulo operator % has the same sign as the second operand.
  • When running code outside a method / class, it is a good practice to put the code inside a main guard. See here for more explanation.

In Python 3.8, the code can be shortened using the assignment operator := together with max function.

if __name__ == "__main__":
    try:
        max_odd = max(o for _ in range(10) 
                      if (o := int(input("Enter a number: "))) % 2 != 0)
        print(f"The largest odd number is: {max_odd}")
    except ValueError:
        print("No odd number was entered")
GZ0
  • 2.4k
  • 8
  • 19