Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If/Else in a Nested Loop | Nested Loops
Python Loops Tutorial
course content

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

If/Else in a Nested Loop

Utilizing if statements remains consistent whether applied within nested loops or in a single while or for loop.

Examine the code below:

123456789101112131415161718
matrix = [[1, -4, 4, 29], [3, 4, -200, -1], [10, -5, 0, 8]] # Changing negative numbers to 'N' i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): if matrix[i][j] > 0: print(matrix[i][j], end = ' ') else: print('N', end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

We've just observed an example of utilizing if/else within a nested loop while handling the matrix.

Now, in your assignment, you'll be dealing with two strings.

Feel free to refer to hints if necessary!

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 3. Capítulo 5
toggle bottom row

If/Else in a Nested Loop

Utilizing if statements remains consistent whether applied within nested loops or in a single while or for loop.

Examine the code below:

123456789101112131415161718
matrix = [[1, -4, 4, 29], [3, 4, -200, -1], [10, -5, 0, 8]] # Changing negative numbers to 'N' i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): if matrix[i][j] > 0: print(matrix[i][j], end = ' ') else: print('N', end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

We've just observed an example of utilizing if/else within a nested loop while handling the matrix.

Now, in your assignment, you'll be dealing with two strings.

Feel free to refer to hints if necessary!

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 3. Capítulo 5
toggle bottom row

If/Else in a Nested Loop

Utilizing if statements remains consistent whether applied within nested loops or in a single while or for loop.

Examine the code below:

123456789101112131415161718
matrix = [[1, -4, 4, 29], [3, 4, -200, -1], [10, -5, 0, 8]] # Changing negative numbers to 'N' i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): if matrix[i][j] > 0: print(matrix[i][j], end = ' ') else: print('N', end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

We've just observed an example of utilizing if/else within a nested loop while handling the matrix.

Now, in your assignment, you'll be dealing with two strings.

Feel free to refer to hints if necessary!

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Utilizing if statements remains consistent whether applied within nested loops or in a single while or for loop.

Examine the code below:

123456789101112131415161718
matrix = [[1, -4, 4, 29], [3, 4, -200, -1], [10, -5, 0, 8]] # Changing negative numbers to 'N' i = 0 # Setting outer while loop to work with the number of rows while i < len(matrix): j = 0 # Setting inner while loop to work with the number of elements in the row while j < len(matrix[i]): if matrix[i][j] > 0: print(matrix[i][j], end = ' ') else: print('N', end = ' ') j +=1 i += 1 print('')
copy

How does the code work?

We've just observed an example of utilizing if/else within a nested loop while handling the matrix.

Now, in your assignment, you'll be dealing with two strings.

Feel free to refer to hints if necessary!

Tarefa

Print all the vowels from the text.

  1. Initialize i.
  2. Configure the outer while loop to iterate through the number of elements in the text.
  3. Configure the inner for loop to iterate through the number of elements in the vowels.
  4. Implement the condition: if an element in the text matches one of the elements in vowels, then print the element.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 5
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt