Skip to content

Latest commit

 

History

History
67 lines (56 loc) · 2.71 KB

File metadata and controls

67 lines (56 loc) · 2.71 KB

Homework 3: Control Flow in Python

Instructions

Complete the following exercises using the code and concepts from workshop_3. All work should be committed to your GitHub repository.

1. Conditional Statements

  • Create a Python script that implements a simple banking application:
    • Define a variable for account balance
    • Define a variable for withdrawal amount
    • Use if-else statements to check if the withdrawal is possible
    • If the withdrawal amount is less than or equal to the balance, print "Withdrawal successful" and show the remaining balance
    • If the withdrawal amount exceeds the balance, print "Insufficient funds" and show the current balance
  • Reference the approach shown in conditionals.py and conditionals_2.py

2. Grade Classification

  • Write a script that assigns letter grades based on a numeric score (0-100):
    • 90-100: A
    • 80-89: B
    • 70-79: C
    • 60-69: D
    • Below 60: F
  • Use elif chains similar to those in chain.py
  • Include appropriate messages for each grade category

3. Ternary Operators

  • Create a script that uses ternary operators for at least two different scenarios:
    • Check if a number is even or odd
    • Check if a person is eligible to vote (age >= 18)
    • Determine if a temperature is above or below freezing
  • Format your output clearly as shown in ternary.py

4. Basic For Loops

  • Write a script that calculates the squares of numbers from 1 to 10 using a for loop
  • Print each number and its square in a readable format
  • Reference the patterns in for_loop_basics.py

5. Range Function

  • Create a script that demonstrates three different ways of using the range function:
    • Print all numbers from 1 to 20
    • Print all even numbers from 2 to 20
    • Print all numbers from 20 to 1 (counting down)
  • Use the techniques shown in range.py

6. String Iteration

  • Write a script that accepts a word from the user and:
    • Prints each character on a separate line
    • Counts the number of vowels (a, e, i, o, u) in the word
    • Prints the word in reverse order
  • Reference the approach in strings.py (from the loops directory)

Submission Guidelines

  1. Create a folder named homework_3 in your GitHub repository.
  2. Create separate .py files for each exercise (7 files total).
  3. Include meaningful comments in your code explaining your logic.
  4. Make sure to commit and push your changes to GitHub.
  5. Test all your scripts to ensure they work as expected.

Evaluation Criteria

  • Proper use of conditional statements
  • Effective implementation of loops and ranges
  • Correct use of ternary operators
  • Proper string manipulation and iteration
  • Appropriate use of the enumerate function
  • Code organization and readability
  • Following the submission guidelines