Describe your understanding of conditional structures and mention 2 situations of everyday life where you could apply this concept.
Conditional Structures: What I understand by conditional structures is that it is a programming construction that executive different code blocks based on whether a certain condition is false or true.
The if-else
statement is the most common conditional structure that I know of which is applied when a condition is true and a specific block of code runs; otherwise, a different block runs. When it comes to the decision-making process and the handling of a wide range of logical operators, conditional structures cannot be overlooked as they are very important for deciding as said earlier.
Two (2) Situations of everyday life of conditional structures:
When it comes to our everyday life application of conditional structures we can apply this concept in the following as mentioned below.
Weather and outfit choice
Cooking and Timing
Weather and Outfit Choice
We all feel cold whenever it is rainy weather right? To this, we can say that; if the weather is rainy, wear a raincoat and carry an umbrella; otherwise, wear normal clothes and skip the umbrella.
if weather == "rainy":
wear("raincoat")
carry("umbrella")
Else:
wear("normal clothes")
Cooking and Timing
I like cooking rice and here I will be using rice to illustrate the conditional structures where I could apply this concept. Having said that my conditional structures will be. If rice has been boiling for 15 minutes, drain it; otherwise, keep boiling until it's done.
if rice_boiling_time >= 15:
drain("rice")
Else:
continue_boiling("rice")
The above two conditional structures allow us to make decisions based on certain conditions, adjusting the action outfit for weather conditions and cooking for eating accordingly.
Create a program that tells the user "Welcome to the room What do you want to do?", if the user writes 1 then it shows a message that says "You have turned on the light, if the user writes 2 then it shows a message that says "you have left the room". Use conditionals.
Below is a very simple that I created in Python using conditionals to achieve the above questions which you can see in the code.
def room():
print("Welcome to the room. What do you want to do?")
print("1: Turn on the light")
print("2: Leave the room")
choice = input("Enter your choice (1 or 2): ")
if choice == "1":
print("You have turned on the light.")
elif choice == "2":
print("You have left the room.")
Else:
print("Invalid choice. Please enter 1 or 2.")
# Call the function
room()
Interpretation
- Here if a user enters 1 it will show a message as "You have turned on the light".
- Here is when a user enters 2 it will show a message as "you have left the room".
From the code, the
room()
function starts by printing a welcome message and providing the two options above which are option 1 and option 2.It takes the user's input with
input()
and stores it in thechoice
variable.The conditional statement
if-elif-else
checks whether the user input is either option 1 or option 2 and shows the corresponding message as we have stated earlier. To this, if the user enters something else, it will show an invalid choice as the message.
Create a program that asks the user for 4 different ratings, calculate the average and if it is greater than 70 it shows a message saying that the section passed, if not, it shows a message saying that the section can improve.
Here I have used Python to create a program that asks the user for 4 different rating ls, calculates the average, and determines if the section passed or can improve. Let's take a look at the code below before seeing how the program is run.
print('Welcome to @josepha post!! Happy reading:)')
def section_ratings():
print("Please enter four different ratings (0-100):")
# Collect four ratings from the user
rating1 = float(input("Enter rating 1: "))
rating2 = float(input("Enter rating 2: "))
rating3 = float(input("Enter rating 3: "))
rating4 = float(input("Enter rating 4: "))
# Calculate the average rating
average = (rating1 + rating2 + rating3 + rating4) / 4
# Display the average
print(f"\nYour average rating is: {average}")
# Determine if the section passed or can improve
if average > 70:
print("The section passed.")
Else:
print("The section can improve.")
# Call the function
section_ratings()
Interpretation
From the out, my rating is as follows!
Rating 1: 70
Rating 2: 75
Rating 3: 80
Rating 4: 85
With the above rating, I got a total of 77.5 as my average rating.The
section_ratings()
function is the function that asks the user to input the four ratings, which are then calculated and displayed to the user.The
if-else
statement is what checks if the average is greater than 70. If it is greater than 70, the message The section passed" is displayed if not "The section can improve" it just as we have seen in the screenshots.
I am inviting: @kouba01, @dove11, and @simonnwigwe,
Cc:-
@alejos7ven
Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Your post has been rewarded by the Seven Team.
Support partner witnesses
We are the hope!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit