Skip to main content

While Loops

Lets start this session with an exercise

# Write a program to print all characters of your first name on console using print function.
  • How many print statements did you write?"
  • What if your name has 10 or 20 letters? Would it be practical to write so many print statements?"

Solution:

first_name = "pawan"
print(first_name[0])
print(first_name[1])
print(first_name[2])
print(first_name[3])
print(first_name[4])

#Number of print statements: 5

Loops

Loops allow us to repeat a block of code for every item in a sequence, like the characters of your name.

Lets solve the above problem using loop:

# Print all characters of the first name using the length
first_name = "Ali"

length_of_str = len(first_name)
i = 0 # Initialize the counter
while i < length_of_str: # Loop until i is less than the length of the name
print(first_name[i]) # Access and print the character at index i
i = i + 1 # Increment the counter

While Loops

Explanation

  1. Initialize the Counter:
i = 0
  • Start with i = 0, which is the index of the first character.
  1. Condition for the Loop
while i < length_of_name:
  • Keep looping as long as i is less than the length of the string.
  1. Access and Print Characters:
print(first_name[i])
  • Use first_name[i] to get the character at index i and print it.
  1. Increment the Counter:
i += 1
  • Increase the value of i by 1 after each iteration to move to the next character.

Debugging Exercises

Please explain what is wrong with the following code snippets. Along with fixing the code, please provide an explanation using comment.

Note: If the code is running for a long time, terminate the program.

# Print all characters of the first name using the length
first_name = input("Please enter your name: ")

length_of_str = len(first_name)
i = 0
while i < length_of_str:
print(first_name[i])
# Print all characters of the first name using the length
first_name = input("Please enter your name: ")

length_of_str = len(first_name)
i = 0
while i < length_of_str:
print(first_name[i])
i = 1 + 0
# Print all characters of the first name using the length
first_name = input("Please enter your name: ")

length_of_str = len(first_name)
while i < length_of_str:
print(first_name[i])
i = i + 1

Coding Exercises

#Prompt the user for a number between 5 and 10.
#Objective 1: Print all integers between 1 and user entered number (included).

#user_input: 5
#Expected output:'
# 1
# 2
# 3
# 4
# 5
#Prompt the user for a number between 5 and 10.
#Objective 1: Print all integers between 1 and user entered number (included) as comma separated values.

#user_input: 5
#Expected output:
# 1, 2, 3, 4 ,5
#Prompt the user for a number between 5 and 10.
#Objective 1: Print all ODD integers between 1 and user entered number (included).
#Objective 2: Print the total integers printed

#user_input: 5
#Expected output:
# 1
# 3
# 5
# Total: 3
#Prompt the user for a number between 5 and 10.
#Objective 1:Print all EVEN integers between 1 and user entered number (included) as comma separated values.
#Objective 2:Print the total integers printed

#user_input: 5
#Expected output:
# 2, 4
# Total: 2
#Prompt the user for a number between 5 and 10.
#Objective 1: If user has entered a number less than 5 or greater than 10,
#prompt the user to reenter the number

#Objective 2: Print all integers between 1
#and user entered number (included).

#user_input: 5
#Expected output:'
# 1
# 2
# 3
# 4
# 5

valid_input = False
while valid_input == False:
# Prompt the user for a number between 5 and 10
user_input = int(input("Enter a number between 5 and 10: "))

# Check if the number is within the valid range
if 5 <= user_input <= 10:
valid_input = True # Set the flag to True if the input is valid
else:
print("The number must be between 5 and 10. Please try again.")

# Print all integers between 1 and the user-entered number (inclusive)
i = 1
while i <= user_input:
print(i)
i += 1

user_input = int(input("Enter a number between 5 and 10: "))
if 5 <= user_input <= 10:
valid_input = True
else:
valid_input = False
print("The number must be between 5 and 10. Please try again.")

max_attempts = 3
attempts = 0
while (valid_input == False and attempts < max_attempts):
# Prompt the user for a number between 5 and 10
user_input = int(input("Enter a number between 5 and 10: "))
attempts = attempts + 1
# Check if the number is within the valid range
if 5 <= user_input <= 10:
valid_input = True # Set the flag to True if the input is valid
else:
print("The number must be between 5 and 10. Please try again.")

# Print all integers between 1 and the user-entered number (inclusive)
i = 1
if (attempts < max_attempts and valid_input == True):
while i <= user_input:
print(i)
i += 1