Adding to the previous review:
- When
xis an integer,abs(x) % 2is equivalent tox % 2in 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")