2

I'm making a simple registrar system. How do I get the grades from my get_grade_points(): function to carry over into my check_graduation(): function?

Here is the code:

CS100 = 'CS100 - Introduction to Programming'
CS200 = 'CS200 - Advanced Programming'
CS300 = 'CS300 - Super Advanced Programming'

student_name = input('What is the students name?')

def main():
    prompt1 = input('Has the student taken CS100? (enter y or n)')
    if prompt1 == 'n':
    print('The student needs to take all three CS courses')
    total_courses = 1

def is_course_taken():
    prompt2 = input('Has the student taken CS200(enter y or n)')
    if prompt2 == 'n':
        print('The student needs to take all three classes')
        total_courses = 2

   prompt3 = input('Has the student taken?(enter y or n)')
    if prompt3 == 'n':
        print('The student needs to take all three classes')
        total_courses = 3

   def get_grade_points():
    A = 4 
    B = 3
    C = 2
    D = 1
    F = 0
    grade1 = input('What grade did the student recieve in CS100?' \
                   '(enter letter grade)')
    grade2 = input('What grade did the student recieve in CS200?' \
                   '(enter letter grade)')
    grade3 = input('What grade did the student recieve in CS300?' \
                   '(enter letter grade)')

def check_graduation(grade1, grade2, grade3):
    gpa = (grade1 * grade2 * grade3) // 3
    if gpa >= 2.5:
        print('Graduation approved')
    else:
        print('Graduation not approved, GPA too low')

    print(student_name)
    print(total_courses)
    print(gpa)

main()

is_course_taken()

get_grade_points()

check_graduation()

I can't figure out how to get the students name from the very beginning and how to get the grades to multiply and calculate the GPA.

If I change the function to this:

def check_graduation(student_name, grade1, grade2, grade3)

the Python interpreter gives me:

TypeError: check_graduation() missing 1 required positional argument: 'get_grade_points'

2 Answers 2

3

I see 2 things right away.

The first is get_grade_points is indented incorrectly. I'm not sure how it's finding anything to run for it, let alone erroring out. Maybe the code is not indented the same as in your code editor?

Second, the last line, check_graduation() is not passing any arguments to check_graduation. You probably want to call it using check_graduation(grade1, grade2, grade3)

I would rewrite your code as follows:

CS100 = 'CS100 - Introduction to Programming'
CS200 = 'CS200 - Advanced Programming'
CS300 = 'CS300 - Super Advanced Programming'


def main():
    student_name = input('What is the students name?')
    prompt1 = input('Has the student taken CS100? (enter y or n)')
    if prompt1 == 'n':
        print('The student needs to take all three CS courses')
    total_courses = 1
    return total_courses, student_name

def is_course_taken():
    prompt2 = input('Has the student taken CS200(enter y or n)')
    if prompt2 == 'n':
        print('The student needs to take all three classes')
        total_courses = 2

    prompt3 = input('Has the student taken?(enter y or n)')
    if prompt3 == 'n':
        print('The student needs to take all three classes')
        total_courses = 3
    return total_courses

def get_grade_value(grade_string):
    if grade_string == 'A':
        return 4.0
    elif grade_string == 'B':
        return 3.0
    elif grade_string == 'C':
        return 2.0
    elif grade_string == 'D':
        return 1.0
    elif grade_string == 'F':
        return 0.0
    return None

def get_grade_points():
    grade1 = None
    while grade1 is None:
        g1_str = input('What grade did the student recieve in CS100?' \
                       '(enter letter grade)')
        grade1 = get_grade_value(g1_str)
    #Repeat updates for other grades as well
    grade2 = input('What grade did the student recieve in CS200?' \
                   '(enter letter grade)')
    grade3 = input('What grade did the student recieve in CS300?' \
                   '(enter letter grade)')
    return grade1, grade2, grade3

def check_graduation(grade1, grade2, grade3, total_courses):
    gpa = (grade1 * grade2 * grade3) // 3
    if gpa >= 2.5:
        print('Graduation approved')
    else:
        print('Graduation not approved, GPA too low')

    print(student_name)
    print(total_courses)
    print(gpa)


total_courses, student_name = main()
#TODO: Put all course questions into is_course_taken function
total_courses = is_course_taken()
grade1, grade2, grade3 = get_grade_points()
check_graduation(grade1, grade2, grade3, total_courses)

This way get_grade_points returns the grades, and check_graduation gets those as inputs to check

EDIT: You also want to return total_courses. I would also not make student_name a global - doing that often leads to mistakes where you unexpectedly use the wrong variable.

EDIT 2: Show example of code that converts string input into a number value.

Sign up to request clarification or add additional context in comments.

1 Comment

I missed that. It's getting the input, which is just a string the user input. It's not converting the strings into numbers.
1

How parameters work is you use them while executing code. For example:

def printHello(stuff):
    print('Hello' + stuff)
def main():
     printHello(world!)
main()

returns Hello world! Also, variables are local(stay inside the function) unless told otherwise. For example:

def defineX():
    x = 10
defineX()
print(x)

will tell you that x is undefined. To avoid this, put global x right below when you are defining your function. You can also at the end of the function put return x. Also, make sure you put correct indentation!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.