简体   繁体   English

Python 3的新功能-无法使程序移至下一个模块

[英]New to Python 3 - Unable to get program to move to the next module

I am new to Python and am trying to get a program to move from one defined module to the next. 我是Python的新手,正在尝试使程序从一个已定义的模块移至下一个模块。 The first module is working just fine but will not move to the def calcAverage module. 第一个模块运行正常,但不会移至def calcAverage模块。 I have attached both the code and the result. 我已经附上了代码和结果。 I only have the sum set up in the first module to verify that it was calculating correctly. 我仅在第一个模块中设置了总和,以验证其计算正确。 This info will be returned and printed into a table. 该信息将被返回并打印到表格中。 (Sorry if I am not using the correct terminology.) I'm very new to this. (很抱歉,如果我使用的术语不正确。)我对此很陌生。

Here is the result: Enter the first test score.89 Enter the second test score.94 Enter the third test score.100 Enter the fourth test score.88 Enter the fifth test score.96 467 结果是:输入第一个测试分数89输入第二个测试分数94输入第三个分数100输入第四个分数88输入第五个分数96 467

> Nothing happens after this. >此后没有任何反应。

Here is the code: 这是代码:

` # This program will calculate the average of five test scores. `#该程序将计算五个测试分数的平均值。

# Print my name
print('Beth Salvatore')

# Assign corresponding letter grade.
A_score = 90
B_score = 80
C_score = 70
D_score = 60

def main():
    # Get five test scores. Get first score.
    test1 = int(input('Enter the first test score.'))
    # Get second score.
    test2 = int(input('Enter the second test score.'))
    # Get third score.
    test3 = int(input('Enter the third test score.'))
    # Get fourth score.
    test4 = int(input('Enter the fourth test score.'))
    # Get fifth score.
    test5 = int(input('Enter the fifth test score.'))
    # Add all scores.
    x = [test1, test2, test3, test4, test5]
    total = sum(x)
    print (total)

    # Return the total of all tests.
    return total

def calcAverage(score1, score2, score3, score4, score5):
    # Find the average of all tests.
    calcAverage = (total) /5.0
    return calcAverage
    print('The average test score is %', calcAverage) 

def determineGrade(score):
    # Assign a letter grade to the average.
    if score >= A_score:
        print('A')
    else:
        if score >= B_score:
            print('B')                           
        else:
            if score >= C_score:
                print ('C')
            else:
                if score >= D_score:
                    print('D')
                else:
                    print('F')

    # Return the letter grade
    return score

    #Print the tests and corresponding grades in a table.
    print('score \t\t numeric grade \t letter grade')
    print('---------------------') \
    print('score 1:' test1, score)
    print('score 2:' test2, score)
    print('score 3:' test3, score)
    print('score 4:' test4, score)
    print('score 5:' test1, score)

# Call the main function.
main()`  

def must be declared before the call of main() . def必须在调用main()之前声明。 You're OK. 没事啦

But you never called calcAverage() nor determineGrade() into the def of your main . 但是,您从未调用过calcAverage(),也从未在您的main的def中确定 Grade()

If your return(total) into your main , then use print(main()) (with no sense at all). 如果返回(总计)main中 ,则使用print(main()) (一点也不意义)。

Or, if you print(total ) in your main , use total=test1+test2+...+test5 before. 或者,如果在main中 打印(总计 ),请在之前使用total = test1 + test2 + ... + test5

That's all at this point. 到此为止。

total = test1+test2+test3+test4+test5 总计= test1 + test2 + test3 + test4 + test5

print (total) 打印(总计)

# Return the total of all tests.
# return total
# because you rarely return a int via a main, until you call the main from elsewhere

Just add determineGrade(total) into your main after print(total) 只需在输出(总计)之后将确定等级(总计)添加到您的主体中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM