函数参数错误 - 缺少1个必需的位置参数

时间:2016-02-02 01:30:44

标签: python-3.x

我正在建立一个简单的注册商系统。如何将get_grade_points():函数中的成绩转入我的check_graduation():函数?

以下是代码:

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()

我无法弄清楚如何从一开始就获得学生的名字,以及如何获得成绩并计算GPA。

如果我将功能更改为:

def check_graduation(student_name, grade1, grade2, grade3)

Python解释器给了我:

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

2 个答案:

答案 0 :(得分:3)

我马上就看到了两件事。

第一个是get_grade_points缩进错误。我不确定它是如何找到任何东西来运行的,更不用说错误了。也许代码与代码编辑器中的代码没有缩进?

其次,最后一行check_graduation()未将任何参数传递给check_graduation。您可能想要使用check_graduation(grade1, grade2, grade3)

来调用它

我会按如下方式重写您的代码:

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)

这种方式get_grade_points会返回成绩,而check_graduation会将这些作为输入来检查

编辑:您还想要返回total_courses。我也不会让student_name成为一个全局性的 - 这通常会导致您意外使用错误变量的错误。

编辑2:显示将字符串输入转换为数字值的代码示例。

答案 1 :(得分:1)

参数的工作原理是在执行代码时使用它们。例如:

#main {
    background-color: #e9e9e9;
    margin: 0;
    padding:1px;
}

返回def printHello(stuff): print('Hello' + stuff) def main(): printHello(world!) main() 此外,除非另有说明,否则变量是本地的(保留在函数内)。例如:

Hello world!

会告诉你x未定义。为避免这种情况,请在定义函数时将def defineX(): x = 10 defineX() print(x) 放在右下方。您还可以在函数末尾放置global x。另外,请确保您输入正确的缩进!

相关问题