Skip to main content
added 1161 characters in body
Source Link
GZ0
  • 2.4k
  • 8
  • 19
if __name__ == "__main__":
    try:# Number generator
    num_gen = (o for _ in range(10) if (o := int(input("Enter a number: "))) % 2 != 0)
    max_odd = max(num_gen, default=None)
    if max_odd is None:
        print("No odd number was entered")
    else:
        print(f"The largest odd number is: {max_odd}")

Wrapping int(input("Enter a number: ")) into a function provides better readability:

def read_input() -> int:
    return int(input("Enter a number: "))

if __name__ == "__main__":
    num_gen = (o for _ in range(10) if (o := read_input()) % 2 != 0)
    max_odd = max(num_gen, default=None)
    if max_odd is None:
        ifprint("No odd number was entered")
    else:
        print(of"The largest odd number is:= {max_odd}")

Another variant that handles invalid user inputs is as follows:

def read_input() -> int:
    while True:
        try:
            return int(input("Enter a number: "))
        except ValueError:
            continue

if __name__ == "__main__":
    try:
        max_odd = max(o for _ in range(10) if (o := read_input()) % 2 != 0)
        print(f"The largest odd number is: {max_odd}")
    except ValueError:
        # Since read_input() no longer raises ValueError, the except
        # statement here only handles the cases where max() gets no inputs
        print("No odd number was entered")
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")
if __name__ == "__main__":
    # Number generator
    num_gen = (o for _ in range(10) if (o := int(input("Enter a number: "))) % 2 != 0)
    max_odd = max(num_gen, default=None)
    if max_odd is None:
        print("No odd number was entered")
    else:
        print(f"The largest odd number is: {max_odd}")

Wrapping int(input("Enter a number: ")) into a function provides better readability:

def read_input() -> int:
    return int(input("Enter a number: "))

if __name__ == "__main__":
    num_gen = (o for _ in range(10) if (o := read_input()) % 2 != 0)
    max_odd = max(num_gen, default=None)
    if max_odd is None:
        print("No odd number was entered")
    else:
        print(f"The largest odd number is: {max_odd}")

Another variant that handles invalid user inputs is as follows:

def read_input() -> int:
    while True:
        try:
            return int(input("Enter a number: "))
        except ValueError:
            continue

if __name__ == "__main__":
    try:
        max_odd = max(o for _ in range(10) if (o := read_input()) % 2 != 0)
        print(f"The largest odd number is: {max_odd}")
    except ValueError:
        # Since read_input() no longer raises ValueError, the except
        # statement here only handles the cases where max() gets no inputs
        print("No odd number was entered")
added 2 characters in body
Source Link
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 shortedshortened 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")

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 shorted 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")

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")
added 24 characters in body
Source Link
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 shorted 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")

Adding to the previous review:

  • 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 shorted 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")

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 shorted 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")
Source Link
GZ0
  • 2.4k
  • 8
  • 19
Loading