what's wrong with `square = lambda n: 2**(n-1) if n>=1 or n<=64 else print("square must be between 1 and 64")` in python

The issue with this square lambda function in Python is related to the conditional statement. The logic is incorrect, which is causing the function to not behave as expected. Below is the corrected version of the square lambda function:

main.py
square = lambda n: 2**(n-1) if 1 <= n <= 64 else print("square must be between 1 and 64")
90 chars
2 lines

Changes made:

  • Replaced or with and logical operator in the conditional statement.
  • Adjusted the comparison operator or equals (<=) to <= on the second part of the condition.

Now, the lambda function will correctly calculate the square based on the input number within the valid range of 1 to 64. If the input number is not within this range, it will print the corresponding error message.

related categories

gistlibby LogSnag