Welcome to Chapter 2! In this chapter, we'll explore the story of Adam and Eve from the Bible and learn about the Boolean data type and conditional statements. As we delve into the story, we'll see how conditional statements can help us understand and organize the different elements involved in the events that took place in the Garden of Eden.
In programming, a Boolean variable is either True or False. It helps us evaluate conditions and make decisions.
For example, let's use a Boolean variable to represent whether a fruit is forbidden or not:
is_forbidden_fruit = True
print(is_forbidden_fruit) # Output: True
In this example, we create a Boolean variable called is_forbidden_fruit and assign the value True to it, indicating that the fruit is forbidden.
In programming, we make decisions based on certain conditions. These conditions can be determined by using Boolean variables as we just talked above. And a Boolean variable is either True or False. To make decisions based on a Boolean variable, we can use the if-else statement. This statement allows us to execute specific sections of the code depending on whether a condition is True or False.
For example, in the story of Adam and Eve, we can use a conditional statement to represent the consequences of their decision to eat from the Tree of the Knowledge of Good and Evil:
ate_from_forbidden_tree = True
if ate_from_forbidden_tree:
print("Adam and Eve are banished from the Garden of Eden.")
else:
print("Adam and Eve remain in the Garden of Eden.")
.gif)
In this example, we use an if statement to check whether the variable ate_from_forbidden_tree is True. If it is, we print a message about their banishment from the Garden. If not, we print a message indicating that they can stay under the else statement. Change True to False, then rerun the program. What happens?
Now, it’s your turn to the conditional statement. Create a Boolean variable named obeyed_command and assign it True or False. Write an if-else statement to check if Adam and Eve obeyed God's command not to eat from the Tree of Knowledge. If they obeyed, print "Adam and Eve obeyed God's command.", otherwise, print "Adam and Eve did not obey God's command."