Day 10: Exercises for both ‘for’ and ‘while’ loops.
Exercise on 'while'

Day 10: Exercises for both ‘for’ and ‘while’ loops.

Objective: You'll get the chance to experiment with loops, solve problems, and witness their magic firsthand.

Loops are your trusty companions in programming, and mastering them opens doors to endless possibilities. So, let's embark on this loop adventure together!

Here are exercises for both for and while loops:

Exercise 1: for Loop - Counting

Objective: Use a for loop to count from 1 to 10 and print each number.

# Exercise: Use a 'for' loop to count from 1 to 10 and print each number.

for number in range(1, 11):
    print(number)        

Exercise 2: for Loop - Summing Numbers

Objective: Use a for loop to calculate the sum of all numbers from 1 to 100.

# Exercise: Use a 'for' loop to calculate the sum of all numbers from 1 to 100.

sum_numbers = 0
for number in range(1, 101):
    sum_numbers += number

print("The sum of numbers from 1 to 100 is:", sum_numbers)        

Exercise 3: while Loop - Countdown

Objective: Use a while loop to count down from 10 to 1 and print each number.

# Exercise: Use a 'while' loop to count down from 10 to 1 and print each number.

count = 10
while count >= 1:
    print(count)
    count -= 1        

Exercise 4: while Loop - Password Prompt

Objective: Use a while loop to repeatedly ask the user for a password until they enter the correct password ('python123').

# Exercise: Use a 'while' loop to prompt the user for a password until they enter the correct one.

correct_password = 'python123'
password = input("Enter the password: ")

while password != correct_password:
    print("Incorrect password. Try again.")
    password = input("Enter the password: ")

print("Access granted!")        

I encourage you to try different examples.

Keep the curiosity alive, and stay ready to tackle new challenges!

Happy coding,

Oluwadunsin

要查看或添加评论,请登录

Janet Oluwadunsin Ogunleye的更多文章

  • Day 4: The Unsung Hero of Analytics—Data Cleaning

    Day 4: The Unsung Hero of Analytics—Data Cleaning

    “Garbage in, garbage out.” This phrase sums up why data cleaning is not just a step but a cornerstone of every…

  • "Belief: The Power to Shape Your Destiny"

    "Belief: The Power to Shape Your Destiny"

    Henry Ford once wisely said, "Whether you believe you can do a thing or not, you are right." These words, uttered by…

    4 条评论
  • Ask Yourself, "What's the Best I Can Do Today?"

    Ask Yourself, "What's the Best I Can Do Today?"

    In the hustle and bustle of our daily lives, it's easy to get caught up in the rush, constantly moving from one task to…

    14 条评论
  • ?? Exciting News! Introducing "Code for Kids" Course ??

    ?? Exciting News! Introducing "Code for Kids" Course ??

    Dear LinkenIn families, I hope you are doing well. As I round up our incredible 15-day programming challenge today, I…

    3 条评论
  • Day 15: Mini Projects - Introduction and Idea Generation

    Day 15: Mini Projects - Introduction and Idea Generation

    Welcome to Day 15 of our Python Programming Challenge! Today, we embark on an exciting journey into the world of…

    11 条评论
  • Day 14: Hands-On Practice: Integrating Functions and Modules

    Day 14: Hands-On Practice: Integrating Functions and Modules

    We've delved into enough theory; now it's time to roll up our sleeves and dive into practical work. Absolutely! We're…

    4 条评论
  • Day 13: Putting It All Together

    Day 13: Putting It All Together

    We're bringing together the knowledge of functions and modules to create Python programs that are well-organized…

    4 条评论
  • Day 12: Modules - Expanding Your Toolbox

    Day 12: Modules - Expanding Your Toolbox

    Modules are like treasure chests, containing valuable tools that extend Python's capabilities. ?? Modules: Python's…

  • Day 11: Functions - The Power of Organizing Code

    Day 11: Functions - The Power of Organizing Code

    Objective: we'll unlock the world of functions, a powerful way to organize and reuse code in Python. ?? Functions: The…

  • Day 9: Control Flow - Introducing Loops

    Day 9: Control Flow - Introducing Loops

    Objective: On Day 9, we're unlocking the power of loops! These are essential tools in Python for automating repetitive…

    2 条评论

社区洞察

其他会员也浏览了