Skip to content

added alternate method in solution_02.py #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions code/solutions/solution_02.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This is a possible solution for exercise_02.py

# Explanation: In line 7 the variable state gets assigned a string
# based on the evaluation result of 'number % 2'.
# If the number is odd, then 'number % 2' is 1, hence True.
# If the number is even, then 'number % 2' is 0, hence False.
# 0 = False, all other numbers = True
# based on the evaluation result of 'number & 1'.
# If the number is odd, then 'number &1' is True.
# If the number is even, then 'number & 1' is False.
# ANDing with 1 is useful for large numbers

number = int(input("Enter a number: "))
state = "odd" if number % 2 else "even"
state = "odd" if number & 1 else "even"

print(f"The number you entered is {state}.")