Coding Lesson (20-24 January)

Monday

Continue on Grade Calculator Project. Today I will learn how to receive input in Python. 
I tried making the code into an HTML but for unknown reason it's bugged. The line that should be inside the code appeared at the web browser. I tried so many things that can help me to get done the things with help of Copilot but unfortunately it doesn't work. Here's the picture of what happening:










The calculate button also doesn't work. How unfortunate.

Tuesday

Continue learning to receive input in Python. Today, I will use another way. Instead of making it into an html, we're going to try using command prompt. Here's the code with the explanations:
# Function to calculate the average of the grades
def calculate_average(grades):
    return sum(grades) / len(grades)

# Function to determine the letter grade based on the average
def determine_letter_grade(average):
    if 90 <= average <= 100:  # A grade
        return 'A'
    elif 80 <= average < 90:  # B grade
        return 'B'
    elif 70 <= average < 80:  # C grade
        return 'C'
    elif 60 <= average < 70:  # D grade
        return 'D'
    else:                      # F grade
        return 'F'

# Main function to take input and calculate grades
def main():
    while True:
        # Prompt the user to enter grades separated by commas
        grades_input = input("Enter your grades separated by commas (e.g., 95, 85, 75, 65): ")

        try:
            # Convert the input string to a list of integers, stripping any whitespace
            grades = [int(x.strip()) for x in grades_input.split(',')]

            # Check if all grades are within the range 0 to 100
            for grade in grades:
                if grade < 0 or grade > 100:
                    raise ValueError("Grades must be between 0 and 100.")

            # Calculate the average of the grades
            average = calculate_average(grades)

            # Determine the letter grade based on the average
            letter_grade = determine_letter_grade(average)

            # Print the results
            print(f"Average Grade: {average:.2f}")
            print(f"Letter Grade: {letter_grade}")
            break  # Exit the loop if input is valid

        except ValueError as e:
            print(f"Error: {e}")
            print("Please try again.")

# If this script is run directly, execute the main function
if __name__ == "__main__":
    main()

Here's some more explanations:
Try-Except Block: We added a try-except block to handle errors.
ValueError: If any of the grades are outside the range of 0 to 100, a ValueError is raised.
Error Message: The script prints an error message if any grades are invalid.
While Loop: We added a while True loop to keep prompting the user until valid input is provided.
Break Statement: The loop exits with the break statement once valid grades are entered and processed.
Error Handling: If a ValueError is raised, an error message is printed, and the user is prompted to try again.

Wednesday

Today, I will continue my Grade Calculator project. Yesterday, I already run it using a command prompt. However, I want to make this project can be used by anyone. Unfortunately, to convert my Python-based Grade Calculator into a web-based application, I need to write an HTML file that incorporates JavaScript for the logic. Since Python cannot run directly in a browser, the logic has to be rewritten in JavaScript. Why? Here's my summary of what I've read: JavaScript dominates the browser because of its early adoption, optimization, and extensive ecosystem. While Python is a powerful language, it wasn't designed for front-end browser interactions. However, you can still use Python indirectly (via back-end servers (like Flask) or transpilers (like PyScript)) to integrate it into web projects. So, here's the result:
Grade Calculator File

Thursday

Migrating my Grade Calculator from Command Prompt to GUI. I'm using Tkinter which is a built-in Python library used to create Graphical User Interfaces (GUIs). It allows developers to build desktop applications with buttons, text boxes, labels, menus, and more—all using Python. After learning a bit about tkinter, here's the result:

Comments

Popular posts from this blog

Beasiswa New Zealand (continue on a new one)

Coding Lesson (18-22 Nov)

Coding Lesson (continue on a new one)