Basic programming course: Lesson #4 Control structures. Part 1

in devjr-s20w4 •  last month 

Hello, my fellow programmers, it is the fourth week of the Steemit Engagement Challenge for season 20 and I'm delighted to be part of this week's contest organized by @alejos7ven which talks about Control Structures Part 1.

OIG2.jpeg
Source

mine.PNG

Describe your understanding of conditional structures and mention 2 situations of everyday life where you could apply this concept.

mine.PNG

Conditional Structures in my little understanding are seen as a set of code structures whose major aim is to allow a certain code to make decisions based on a certain requirements. This means that before a code can execute a certain condition must be met and what is responsible for this is what we call the control structure.

There are different control structures which include the if statement, else statement and the if else statement. These conditions are designed such that if one condition is not met, the code should go ahead to check the other conditions and return a true of false value depending on the requirements that is met.

2 situations of everyday life

1). Decision based on wheather condition: Here we are looking at working to the farm if it rains else we work from home if there is no rain. So putting this to the system, we have a code of the format below

if weather == "rainy":
work("farm")
Else:
work("home")

2). Woman and Delivery Time: Here, the right time for a woman to deliver is 9 months else the pregnancy is suppose to remain till the period of delivery. So putting this to the system, we have a code of the format below

if pregnancy == "9_Months":
woman("deliver")
Else:
woman("remain-pregnant")

mine.PNG

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.

mine.PNG

To perform this operation, I will be using the python IDE. So in this function, the first thing you will see when you run this code Welcome to the room. What do you want to do?

This is a question that will require you to enter a number 1 or 2. If you enter the number 1, it will come up with the message Turn on the light but if you enter the number 2 it will come up with the message Leave the room and finally, if you enter a number different from 1 and 2 then it will show Invalid choice. Please enter 1 or 2. So below is the code structure used for this function.

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()

IMG_20241005_103919.jpg

IMG_20241005_103946.jpg
Entering 1 as Choice

IMG_20241005_103919.jpg

IMG_20241005_104122.jpg
Entering 2 as Choice

IMG_20241005_103919.jpg

IMG_20241005_120209.jpg
Entering 3 as Choice

mine.PNG

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.

mine.PNG

In this task, we are going to be looking at how the system will require a user to enter rating 1,2,3 & 4. And the system will go ahead calculate the average of the four numbers entered and after which, the if condition will take place.

According to the condition, if the average > 70, it must print the section passed else it will print the section can improve. Now let's look at the program and the results obtain in both conditions.

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()

IMG_20241005_104022.jpg

IMG_20241005_104038.jpg
sectioned passed

IMG_20241005_104022.jpg

IMG_20241005_104008.jpg
sectioned can improve

This is indeed a nice course as it reminds me a lot. I want to finally invite a few friends to also join the contest @josepha, @suboohi, and @solaymann.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...

Upvoted! Thank you for supporting witness @jswit.

Upvoted. Thank You for sending some of your rewards to @null. It will make Steem stronger.

TEAM 7

Congratulations!

THE QUEST TEAM has supported your post. We support quality posts, good comments anywhere, and any tags


postbanner.JPG

Curated by : @sduttaskitchen

this is python, right?