# Sam, your friend wants to print his dog's name,Snoopy, five times.
# He asks you to help him, as you are learning to code in Python
# Can you help him?
'''Task 1: Repetition'''
print("**** Task 1: ****")
print()
# What is the statement you would use to print the name of the dog
# You use the print command to display any output on the terminal screen. Can you help your friend and write the code to print the dog’s name?
# One of the ways is to use 5 print statements like this:
print ("Snoopy")
print ("Snoopy")
print ("Snoopy")
print ("Snoopy")
print ("Snoopy")
# Now, Sam wants to print the dog's name a 100 times? Would you want to write print statements 100 times? Isn’t it too lengthy ?
# Do we have a hack?
# Uncomment the next line to try the hack
print("Snoopy\n" * 100)
# Your friend Sam is enjoying it and now wants you to print numbers from 1 to 5. Easy isn't it!!
# Uncomment the next 5 lines
print(1)
print(2)
print(3)
print(4)
print(5)
# Sam now wants you to print numbers from 1 to 100.
# Are you thinking of 100 print statements or a hack like we used for printing Hello?
# Remember print ("Hello\n" * 100)?
# Well, no! That does not work. There isn’t such a hack for printing different numbers.
# We have seen there are certain tasks that are:
# repetitive
# have a pattern
# takes a lot of human effort
# You must be wondering if Python can do these repetitive tasks on its own using some commands.
# Well yes!! Python uses loop commands for these tasks.
# Loop allows you to do something again and again.
# One of the Python loop commands is the while
# In the while loop, the set of commands are repeated till a condition is true.
# The structure of the while loop is:
'''while <condition>:
code to be repeated
code to be repeated
Code outside the loop '''
# Note the colon and indentation in the structure. It is quite similar to the if statement
# To summarize, the steps of a while loop are as follows:
# Check the condition.
# Execute the code in the block.
# Repeat
# Let's take an example. Suppose we want to print Sam’s dog’s name five times. Here the:
# Condition -> Keeping a count for 5 times (using a counter)
# Code to execute -> Printing Dog’s name(Snoopy)
# Repeat -> Repeat for 5 times (incrementing the counter)
# Uncomment the next 4 lines to see the while loop in:
counter = 1 # initialize a counter variable
while counter <= 5: # loop condition
print("Snoopy") # code to execute
counter = counter + 1
# Click on Run and see what happens
'''Task 2: Print While You Can'''
print("**** Task 2: ****")
print()
# Sam is having fun learning with you. He wants you to write a program to print the first 100 numbers.
# Can you do that?
x = 0
while x != 100:
x+=1
print(x)
# Wow!! Now that you have printed the first 100 numbers, lets try a twist.
# Let's get the sum of the first hundred numbers. Can you tell me what in the program needs to change for this to happen?
# In addition to the counter, you will have to create another variable which will keep adding each number to it.
# Similar to a bag where we collect goodies. As you keep getting the goodies, you keep adding it to that bag.
# The number of items in the shopping bag will increment by 1, which is the counter and the price of each article keeps adding to the total amount which will be the accumulator.
# So you can call that variable “total” and add the counter values to it, like:
# total=total+counter
'''Task 3: Print Within Limits'''
print("**** Task 3: ****")
print()
# The previous task has made Sam interested to write a program where he can find the sum of numbers from 1 to a number that the user wants. Can you help him?
# Hint: You can take that limit as input from the user, every time the program runs
y = int(input("Enter a number and we will give you the sum of it and all the numbers before it: \n"))
b = 0
z = 0
while b!= y:
b+=1
z+=b
print(z)
'''Task 4: Reverse Printi'''
print("**** Task 4: ****")
print()
# Sam is really loving the way he is able to play with numbers using the while loop.
# He now wants to understand if he can do a reverse printing of numbers i.e.backward counting
# He wants you to write a program that will print the first 50 numbers backwards.
# Hint: Instead of assigning 0 to the counter, set it to 50, and decrement the counter(i.e. subtract by 1)
counter = 50 # initialize a counter variable
while counter != 0: # loop condition
print(counter) # code to execute
counter = counter - 1
'''Great! You have made a great start with the while loop commands. You are ready for the next set of challenges.'''
To embed this project on your website, copy the following code and paste it into your website's HTML: