python - 无论输入如何,打印语句默认为第一个“if”语句
问题描述
我对 python 非常陌生,所以这段代码可能是一团糟,但我遇到的问题是无论计算什么平均值,代码都在执行第一个“if”语句(即计算的平均值是76.0,但它仍在执行“A”级,而不是“C”级)。我究竟做错了什么?
此外,为了提供信息,代码应该让用户输入四个测试分数,代码必须在 70-85 范围内随机化第五次测试的分数,计算并返回所有五个测试分数的平均值并分配基于平均值的等级。
def rand():
import random
num5 = random.randint(70,85)
calcAverage(num5)
def calcAverage(num5):
num1 = float(input("Enter the first test score: "))
num2 = float(input("Enter the second test score: "))
num3 = float(input("Enter the third test score: "))
num4 = float(input("Enter the fourth test score: "))
print("\nThe random test score generater on your behalf was", num5)
total = num1 + num2 + num3 +num4 + num5
average = total // 5
letterGrade(average)
def letterGrade(average):
if average >= 90.00 and average <= 100.00:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is A.")
elif average >= 80.00 and average <= 89.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is B.")
elif average >= 70.00 and average <= 79.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is C.")
elif average >= 60.0 and average <= 69.99:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is D.")
else:
print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is F.")
rand()
解决方案
推荐阅读
- amazon-web-services - AWS Batch Transform 中 CSV 文件的输出筛选器
- json - 从 Json 解析数据数据文件中的值
- npm - npm i 导致错误:缺少必需的参数 #1
- c# - .Net Core JsonElement 解析多嵌套动态 JSON
- java - 在不触发 Hibernate 延迟加载的情况下访问哈希码
- amazon-web-services - Cloud Formation 条件资源属性 AWS Backup Resource
- python - 当 x.replace("y", " ") 但 "y" 仍在列表中时没有错误
- python - numpy 数组中的索引
- python - Docker POSTGRESQL - 使用字符串和字节数据插入多行
- snowflake-cloud-data-platform - Snowflake & GoodData 错误“所有项目加载失败”