Practice questions on loops in python

Introduction

Loops in the program are used to iterate over the sequence of elements. In python, there are two primitive loops commands i.e. 

There are four practice questions and two coding challenges to give a good practice on loops in python.

Question 1: Sum of numbers

Write a program to find the sum of all the numbers less than a number entered by the user. Display the sum. The expected output is attached below.

Source code:
tot = 0
num = int(input("Enter a limit:"))
for i in range(num):
    tot = tot + i
    print(i, end=" ")
print(f"\nTotal={tot}")

Output:

Enter a limit:7
0 1 2 3 4 5 6 
Total=21
Question 2: Magic Number

Write a program that makes the user guess a particular number between 1 and 100. Save the number to be guessed in a variable called magic_number.
If the user guesses a number higher than the secret number, you should say Too high!. Similarly, you should say Too low! if they guess a number lower than the secret number. Once they guess the number, say Correct!
Expected Output:

Source code:
magic_number = 50
while True:
    guess = int(input("Enter the number (1-100): "))
    if guess > magic_number:
        print("That number is too high!")
        continue
    elif guess < magic_number:
        print("That number is too low!")
        continue
    elif guess == magic_number:
        print("Hooray! You got it correct")
        break

Output:

Enter the number (1-100): 45
That number is too low!
Enter the number (1-100): 60
That number is too high!
Enter the number (1-100): 50
Hooray! You got it correct
Question 3: Guess the number

Write a program where the user enters a number. If the number matches the number, acknowledge the user and stop the program. If the number does not match, keep asking for the next number.
Expected Output:

Source code:
secret_number = 1234
while True:
    your_guess = int(input("Enter your guess:"))
    if secret_number == your_guess:
        print("Your guess is correct!")
        break
    else:
        print("Your guess is wrong. Try Again!\n")

Output:

Enter your guess:45
Your guess is wrong. Try Again!

Enter your guess:23
Your guess is wrong. Try Again!

Enter your guess:1234
Your guess is correct!
Question 4: Rolling a dice

Write a program that will print out all combinations that can be made when 2 dice are rolled. And should continue until all values up to 6, and 6 are printed. (Hint: You should have a space after each comma!)
Expected Output:

Source code:

for i in range(1,7):
    for j in range(1, 7):
        print(i,", ",j)
    print()

Output:

1 ,  1
1 ,  2
1 ,  3
1 ,  4
1 ,  5
1 ,  6

2 ,  1
2 ,  2
2 ,  3
2 ,  4
2 ,  5
2 ,  6

3 ,  1
3 ,  2
3 ,  3
3 ,  4
3 ,  5
3 ,  6

4 ,  1
4 ,  2
4 ,  3
4 ,  4
4 ,  5
4 ,  6

5 ,  1
5 ,  2
5 ,  3
5 ,  4
5 ,  5
5 ,  6

6 ,  1
6 ,  2
6 ,  3
6 ,  4
6 ,  5
6 ,  6
Coding challenge 1: Multiplication Table

Write a program to print a multiplication table to 12 of a given number.
Expected Output:

Source code:

num = int(input("Enter a number:"))
print("MULTIPLICATION TABLE OF ", num)
print("-----------------------")
for i in range(1, 13):
    print(f"{i} * {num} = {i * num}")

Output:

Enter a number:9
MULTIPLICATION TABLE OF  9
-----------------------
1 * 9 = 9
2 * 9 = 18
3 * 9 = 27
4 * 9 = 36
5 * 9 = 45
6 * 9 = 54
7 * 9 = 63
8 * 9 = 72
9 * 9 = 81
10 * 9 = 90
11 * 9 = 99
12 * 9 = 108
Coding challenge 2: Printing pattern

Write a program to print the following two patterns implementing the nested loop concept.

Source code:

for i in range(5):
    for j in range(0, i+1):
        print("*", end=" ")
    print()

print("\n")

for i in range(5):
    for j in range(0, i+1):
        print("*", end=" ")
    print()
for i in range(6):
    print("*", end=" ")
print()  
for i in range(5, 0, -1):
    for j in range(1, i+1):
        print("*", end=" ")
    print()

Output:

* 
* * 
* * * 
* * * * 
* * * * * 


* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
*