Unit 4, Python:: TEALS : LOOPS, WHILE LOOPS

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

RECURSIVE: What is recursion used for?

Recursion lets us tackle complex problems by reducing the problem to a simples one .

How are while loops and for loops different in Python?

While loops iterate while a condition is true. For loops iterate through a sequence of elements

What are while loops in Python?

While loops let the computer execute a set of instruction while the condition is true (Using while loops we can keep executing the same group of instructions until the condition stops being true.)

In this code, there's an initialization problem that's causing our function to behave incorrectly. Can you find the problem and fix it? def count_down(start_number): while current > 0: print (current) current -= 1 print ("Zero!") count_down(3)

def count_down(current): while current > 0: print (current) current -= 1 print ("Zero!") count_down(3)

The following code causes an infinite loop. Can you figure out what's missing and how to fix it? def print_range(start, end): # Loop through the numbers from start to end n = start while n <= end: print(n)

def print_range(start, end): # Loop through the numbers from start to end n = start while n <= end: print(n) n += 1 Loop (cycle) begins from start number to the stop number. In example we have 1 and 5 respectively. Start = 1 to the end 5. At the while-loop's body you can see print(n) function to print number, after printing number will increase to the 1 and the loop will start again until the condition n<=end is met

Write a script that prints the multiples of 7 between 0 and 100. Print one multiple per line and avoid printing any numbers that aren't multiples of 7. Remember that 0 is also a multiple of 7.

for i in range(0,100,7): print(i)

Write a script that prints the first 10 cube numbers (x**3), starting with x=1 and ending with x=10.

for x in range(1,11): print(x**3)

How many times will "Not there yet" be printed? x = 0 while x < 5: print("Not there yet, x=" + str(x)) x = x + 1 print("x=" + str(x))

5 (The variable x starts at 0 and gets incremented once per iteration, so there are 5 iterations for which x is smaller than 5.)


संबंधित स्टडी सेट्स

Managerial Accounting Ch 2 (3 non-calculation questions)

View Set

Sociology 2e Chapter 9, 10, 11, and 12

View Set

Chapter 25- Disorders of Renal Function

View Set