Ask Question
5 April, 14:13

Write a function called stop_at_four that iterates through a list of numbers. Using a while loop, append each number to a new list until the number 4 appears. The function should return the new list.

+4
Answers (1)
  1. 5 April, 18:12
    0
    def stop_at_four (input_list):

    output_list = []

    index = 0

    while index < len (input_list):

    if input_list[index]! = 4:

    output_list. append (input_list[index])

    index + = 1

    else:

    break

    return output_list

    Explanation:

    The function stop_at_four has an argument (input_list) which is the list of numbers we want to test.

    The output_list is the list of the new numbers that will be generated.

    index=0 is the counter with an initial value of 0

    The while loop contains an if statement that ensures that a number that is being checked on the input list which is not equal to 4 is appended to the output_list and the counter increases.

    The while loop encounters the break statement once a number 4 appears in the list of numbers.

    It returns the output_list which contains numbers without 4.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function called stop_at_four that iterates through a list of numbers. Using a while loop, append each number to a new list until ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers