Skip to content

Instantly share code, notes, and snippets.

@ReaganS94
Created February 6, 2025 14:27
Show Gist options
  • Save ReaganS94/080defc019614706711c76722192ad57 to your computer and use it in GitHub Desktop.
Save ReaganS94/080defc019614706711c76722192ad57 to your computer and use it in GitHub Desktop.

Exercise 1: For Loop with Continue and Break

Instructions:

  • Create a list of integers from 1 to 15.
  • Iterate over the list with a for loop.
  • If the current number is divisible by 3, use continue to skip the print.
  • If the current number is greater than 10, use break to exit the loop immediately.
  • Otherwise, print the current number.

Hint:
Think about the order. Make sure your break happens when the condition is met before doing anything else!

Exercise 2: While Loop with Continue, Break, and Else

Instructions:

  • Initialize a variable counter to 0.
  • Create a while loop that continues as long as counter is less than 10.
  • Inside the loop:
    • If counter equals 5, increment the counter and use continue to skip printing that value.
    • If counter equals 8, use break to exit the loop.
  • Otherwise, print the value of counter.
  • Include a while ... else clause that prints a message like "Loop completed without break" if the loop finishes naturally.

Hint:
Remember what happens if you forget to update the counter 👻

Exercise 3: List Comprehension

Instructions:

  • Create a list called numbers containing integers from 1 to 20.
  • Use a list comprehension to generate a new list that contains the squares of only the even numbers from numbers.
  • Print the resulting list.
  • Extra Challenge: Make it so that your list comprehension includes only even numbers that are also greater than 10 before squaring them.

Exercise 4: If-Elif-Else Decision Making

Instructions:

  • Prompt the user to enter a numerical grade (an integer between 0 and 100).
  • Using if/elif/else statements, evaluate the grade and print the corresponding category:
    • If the grade is greater than 100 or less than 0, print "Invalid grade".
    • If the grade is 90 or above, print "Excellent".
    • If the grade is between 70 and 89 (inclusive), print "Good".
    • If the grade is between 50 and 69 (inclusive), print "Average".
    • If the grade is below 50, print "Fail".
  • Test your code by entering different values. Hint:
  • Look up how to take user input. Specifically, look up what data-type you get back when getting a user input and act accordingly.
  • Consider testing edge cases such as entering a negative number or a number greater than 100. Remember, never ever trust the user.

Exercise 5: Comparisons in a While Loop

Instructions:

  • Set a secret number (something like secret_number = 7).
  • Create a while loop that repeatedly prompts the user to guess the secret number. (hint)
  • Use comparison operators to check if the user's guess is:
    • Too low (if guess is less than the secret number)
    • Too high (if guess is greater than the secret number)
    • Correct (if the guess equals the secret number)
  • Print an appropriate message for each case.
  • When the correct guess is made, exit the loop and congratulate the user. 🤝

Exercise 6: Match-Case Statement

Instructions:

  • Simulate a simple vending machine selection. Prompt the user to input a selection letter (A, B, C, D, or E). (Make sure to convert the input to uppercase for consistency.)
  • Use a match-case statement to decide which item to dispense based on the selection:
    • A: Print "Chips dispensed".
    • B: Print "Chocolate Bar dispensed".
    • C: Print "Soda dispensed".
    • D: Print "Candy selected".
      • Then, prompt the user with an additional question: "Would you like extra dip with your candy? (Yes/No):".
      • If the answer is "Yes" (case-insensitive), print "Candy with extra dip dispensed".
      • Otherwise, print "Candy dispensed".
    • E: Print "Sandwich dispensed".
    • For any other input, print "Invalid selection, please try again".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment