# Sam wants you to meet his friend Math πrate.
# Math πrate is taking part in a coding quest, and wants you to join in that quest.
# Sam told his Math πrate friend that you are very good at coding.
# So are you ready to get on to the quest?
''' Task 1: Numerical Trial '''
print("**** Task 1: ****")
print()
# The first stage of the quest is a trial run.
# The quest is asking you to analyze and observe the program output.
# Uncomment the statements below and click Run
for i in range(15):
if (i % 2) == 0:
print(i,"is a even number")
else:
print(i,"is a odd number")
# What exactly happened in the program?
# Can you identify any new command in the program?
# Excellent!! It is the for statement.
# The for loop executes the code inside the loop for a fixed number of items.
# Let's look at another example. Uncomment the statement below and click Run
print("Python range() example")
print("Get numbers from 0 to 6")
for i in range(6):
print(i)
# If you look at the output generated, you will notice that the numbers in the range of 0 to 6 is printed:
# We got integers from 0 to 5 because range() function does not include the last (stop) number in the result.
# The range() function, takes three arguments: range(start, stop[, step])
# Out of the three , 2 arguments are optional, that is, start and step are the optional arguments.
# A start argument is a starting number of the sequence. By default, it starts with 0 if not specified.
# A stop argument is an upper limit that is, generate numbers up to this number. The range() doesn’t include this number in the result.
# The step is the increment number. The default value of the step is 1 if not specified.
# Uncomment the statement below and click Run:
x=0
##define a while loop
while(x <4):
print(x)
x = x+1
#Define a for loop
for x in range(4):
print(x)
# So what is the difference between while and for loops?
# while loop iterates till a condition is true or false. The for Loop iterates for the specified range.
# “While loop” is like filling a bucket with water using a mug. You never know how many mugs of water you use, but only be stopped when the bucket is full. Work until satisfying a given condition.
# Whereas “for loop” is like filling a bucket with ten mugs of water, here the ten is a condition that we have to satisfy.
'''Task 2: Who is the odd one out'''
print("**** Task 2: ****")
print()
# Math πrate is ready to take the next small steps in the coding quest.
# Write a program that prints the odd numbers that occur between the 10 and 20
for x in range(10,21):
if x % 2 == 0:
continue
else:
print(x)
'''Task 3: Ready to Skip'''
print("**** Task 3: ****")
print()
# Do you remember the program that you wrote on skip counting?
# Uncomment the statement below and click Run
numstart = int(input("Enter the starting number: "))
numend=int(input("Enter the ending number: "))
cnt=int(input("Enter the skip count number: "))
for i in range(numstart,numend,cnt):
print(i)
# What did you observe?
'''Task 4: Sum and Product'''
print("**** Task 4: ****")
print()
# Math πrate is very happy with your help.
# You need to assist Math πrate in writing a program that takes a starting and ending number and then calculates the:
# Total sum of the numbers between the starting and ending number
# Product of the numbers between the starting and ending number
numstart = int(input("Enter the starting number: "))
numend=int(input("Enter the ending number: "))
sum = 0
pro = 1
for y in range(numstart,numend+1):
sum += y
pro*=y
print(sum)
print(pro)
'''Task 5: Lets get Even'''
print("**** Task 5: ****")
print()
# Math πrate has been working with each of the coding quest tasks effortlessly with your help.
# He has been given another number based quest.
# He needs your help in writing a program that takes :
# A starting and and ending number as the input from the user
# For each number between that range:
# If the number is an even number, then the output must display the square of the number
# If the number is odd, then the output must display the cube of the number
# Are you ready to help him with the program?
numstart = int(input("Enter the starting number: "))
numend=int(input("Enter the ending number: "))
for i in range(numstart,numend+1):
if (i % 2) == 0:
print(i**2)
else:
print(i**3)
'''Great! You have made a great headstart with the for loops.'''
To embed this project on your website, copy the following code and paste it into your website's HTML: