File size: 1,085 Bytes
50aeb90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# app.py
import streamlit as st

def calculate_gpa(grades, credits):
    total_points = 0
    total_credits = 0
    for grade, credit in zip(grades, credits):
        total_points += grade * credit
        total_credits += credit
    return total_points / total_credits if total_credits > 0 else 0

def main():
    st.title("GPA Calculator")

    st.write("Enter your grades and corresponding credits to calculate your GPA.")

    num_courses = st.number_input("Number of courses:", min_value=1, step=1, value=1)

    grades = []
    credits = []

    for i in range(num_courses):
        st.write(f"Course {i+1}:")
        grade = st.number_input(f"Grade for Course {i+1} (e.g., 4.0 for A):", min_value=0.0, max_value=4.0, step=0.1, key=f"grade_{i}")
        credit = st.number_input(f"Credits for Course {i+1}:", min_value=0.0, step=0.5, key=f"credit_{i}")
        grades.append(grade)
        credits.append(credit)

    if st.button("Calculate GPA"):
        gpa = calculate_gpa(grades, credits)
        st.success(f"Your GPA is: {gpa:.2f}")

if __name__ == "__main__":
    main()