Syntax Master
Trace the Output
Concept Check
Debug Detective
Code It!
100

What is the syntax for a for loop in python?

for <variable> in <sequence>:

<code block>

100

What will this code print?

x = 5 

y = 3 

print(x + y) 

print(x * 2)  

10

100

What is the difference between a parameter and an argument?

  • Parameter: A variable in the function definition that receives a value
  • Argument: The actual value passed to the function when it's called
100

Write a function called double that takes one parameter (a number) and returns double that value.

def double(num):    

    return num * 2

200

Write the syntax for defining a function that takes two parameters and returns a value.

def function_name(parameter1, parameter2):    <code>    

return value

200

What will this code print? 

count = 0 

while count < 3:    

    print(count)    

    count += 1 

print("Done")

Done

200

When should you use a while loop versus a for loop?

  • Use a while loop when:
    • You don't know how many iterations you need in advance
    • The loop depends on a condition that might change unpredictably
    • You're waiting for user input or a specific event
    • Example: Input validation, sentinel-controlled loops
  • Use a for loop when:
    • You know exactly how many iterations you need
    • You're iterating over a sequence (range, string, list)
    • You need a counter variable
    • Example: Counting from 1 to 10, processing each character in a string
200

Write a function called is_even that takes an integer and returns True if the number is even, False if it's odd.

def is_even(num):

    return num % 2 == 0


300

Explain the difference between local and global variables.

  • Local variables: Created inside a function and only exist within that function's scope. They are destroyed when the function returns.
  • Global variables: Created outside functions and exist throughout the entire program.
300

What will this code print?

def multiply(a, b):    

    result = a * b    

return result answer = multiply(4, 5) print(answer) 

print(multiply(3, 3))  

20 

9

300

Explain what "scope" means in programming. Why is it important?

What different types of scope  

Scope refers to the region of a program where a variable can be accessed or modified.

  • Local scope: Variables created inside a function, only accessible within that function
  • Global scope: Variables created outside functions, accessible throughout the program

 

300

Write a program that asks the user for a number and prints all numbers from 1 to that number (inclusive) using a for loop.

user_num = int(input("Enter a number: ")) 

for i in range(1, user_num + 1):    

print(i)

400

Write the complete syntax for a nested loop structure. Explain how the inner and outer loops interact and when you would use this pattern.

for outer_var in range(n):    

for inner_var in range(m):        

<code>

The inner loop completes ALL its iterations for EACH iteration of the outer loop.  

400

What will this code print? 

for i in range(3):   

     for j in range(2):        

        print(f"i={i}, j={j}")


i=0, j=0

i=0, j=1  

i=1, j=0 

i=1, j=1 

i=2, j=0 

i=2, j=1

400

What is the difference between a void function and a value-returning function? Give an example of when you'd use each.

Answer: Void Function:

  • Does NOT return a value
  • Performs an action (like printing, modifying global variables, file operations)
  • Called as a statement

Value-Returning Function:

  • Returns a value using return statement
  • Computes and sends back a result
  • Called as part of an expression
400

Write a program that asks the user for numbers until they enter -1 (sentinel value). Calculate and display the average of all numbers entered (excluding the -1).

total = 0 

count = 0 

num = int(input("Enter a number (-1 to stop): "))

while num != -1:  

    total += num

    count += 1 

    num = int(input("Enter a number (-1 to stop): ")) 

if count > 0: 

    average = total / count 

    print(f"Average: {average}") 

else: 

    print("No numbers entered") 

500

Question: Explain the syntax and behavior of the range() function with all three possible arguments. Write examples showing range() with 1, 2, and 3 arguments. Then explain how you would use range() to generate: (a) numbers counting backwards, (b) only even numbers, (c) every third number.

range(start, stop, step)

for i in range(10, 0, -1):    

print(i)  

for i in range(0, 21, 2):    

print(i)

for i in range(1, 21, 3):    

print(i)  

500

What will this code print?

total = 0

def calculate(x):    

    global total    

    total += x    

    return total * 2 

result1 = calculate(5) 

print(result1) 

result2 = calculate(3) 

print(result2) 

print(total)

10 

16 

8

500

Explain the concept of "input validation" and write a complete example that validates user input for a number between 1 and 100 (inclusive).

500

Question: Write a menu-driven program that maintains a running total. The program should:

  • Display a menu with options: (1) Add a number, (2) Subtract a number, (3) Multiply by a number, (4) Display current total, (5) Reset to zero, (6) Quit
  • Use a while loop to keep showing the menu until the user quits
  • Validate that menu choices are 1-6
  • Use separate functions for each mathematical operation that take the current total and the user's number as parameters and return the new total
  • Start with a total of 0

https://pythontutor.com/render.html#code=def%20display_menu%28%29%3A%0A%20%20%20%20%22%22%22Displays%20the%20menu%20options%22%22%22%0A%20%20%20%20print%28%22%5Cn%3D%3D%3D%3D%3D%20Calculator%20Menu%20%3D%3D%3D%3D%3D%22%29%0A%20%20%20%20print%28%221.%20Add%20a%20number%22%29%0A%20%20%20%20print%28%222.%20Subtract%20a%20number%22%29%0A%20%20%20%20print%28%223.%20Multiply%20by%20a%20number%22%29%0A%20%20%20%20print%28%224.%20Display%20current%20total%22%29%0A%20%20%20%20print%28%225.%20Reset%20to%20zero%22%29%0A%20%20%20%20print%28%226.%20Quit%22%29%0A%20%20%20%20print%28%22%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%22%29%0A%0Adef%20add%28total,%20num%29%3A%0A%20%20%20%20%22%22%22Adds%20num%20to%20total%22%22%22%0A%20%20%20%20return%20total%20%2B%20num%0A%0Adef%20subtract%28total,%20num%29%3A%0A%20%20%20%20%22%22%22Subtracts%20num%20from%20total%22%22%22%0A%20%20%20%20return%20total%20-%20num%0A%0Adef%20multiply%28total,%20num%29%3A%0A%20%20%20%20%22%22%22Multiplies%20total%20by%20num%22%22%22%0A%20%20%20%20return%20total%20*%20num%0A%0Adef%20get_valid_choice%28%29%3A%0A%20%20%20%20%22%22%22Gets%20a%20valid%20menu%20choice%20%281-6%29%22%22%22%0A%20%20%20%20choice%20%3D%20int%28input%28%22Enter%20your%20choice%20%281-6%29%3A%20%22%29%29%0A%20%20%20%20while%20choice%20%3C%201%20or%20choice%20%3E%206%3A%0A%20%20%20%20%20%20%20%20print%28%22Invalid%20choice!%22%29%0A%20%20%20%20%20%20%20%20choice%20%3D%20int%28input%28%22Enter%20your%20choice%20%281-6%29%3A%20%22%29%29%0A%20%20%20%20return%20choice%0A%0Adef%20get_number%28%29%3A%0A%20%20%20%20%22%22%22Gets%20a%20number%20from%20the%20user%22%22%22%0A%20%20%20%20return%20float%28input%28%22Enter%20a%20number%3A%20%22%29%29%0A%0A%23%20Main%20program%0Atotal%20%3D%200%0Arunning%20%3D%20True%0A%0Awhile%20running%3A%0A%20%20%20%20display_menu%28%29%0A%20%20%20%20choice%20%3D%20get_valid_choice%28%29%0A%20%20%20%20%0A%20%20%20%20if%20choice%20%3D%3D%201%3A%0A%20%20%20%20%20%20%20%20num%20%3D%20get_number%28%29%0A%20%20%20%20%20%20%20%20total%20%3D%20add%28total,%20num%29%0A%20%20%20%20%20%20%20%20print%28f%22New%20total%3A%20%7Btotal%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20elif%20choice%20%3D%3D%202%3A%0A%20%20%20%20%20%20%20%20num%20%3D%20get_number%28%29%0A%20%20%20%20%20%20%20%20total%20%3D%20subtract%28total,%20num%29%0A%20%20%20%20%20%20%20%20print%28f%22New%20total%3A%20%7Btotal%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20elif%20choice%20%3D%3D%203%3A%0A%20%20%20%20%20%20%20%20num%20%3D%20get_number%28%29%0A%20%20%20%20%20%20%20%20total%20%3D%20multiply%28total,%20num%29%0A%20%20%20%20%20%20%20%20print%28f%22New%20total%3A%20%7Btotal%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20elif%20choice%20%3D%3D%204%3A%0A%20%20%20%20%20%20%20%20print%28f%22Current%20total%3A%20%7Btotal%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20elif%20choice%20%3D%3D%205%3A%0A%20%20%20%20%20%20%20%20total%20%3D%200%0A%20%20%20%20%20%20%20%20print%28%22Total%20reset%20to%200%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20elif%20choice%20%3D%3D%206%3A%0A%20%20%20%20%20%20%20%20print%28%22Goodbye!%22%29%0A%20%20%20%20%20%20%20%20running%20%3D%20False&cumulative=false&heapPrimitives=nevernest&mode=edit&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false