Python 脚本的意外输出
Unexpected Output from Python Script
我希望有人可以帮助解决我在使用 Python 脚本时遇到的意外输出问题。
我似乎无法让“if-elif”条件语句分别正确处理整数范围 64 - 69 和 72 - 77。
下面这个非常简单的脚本的目的是将学生的数字考试分数转换为匹配的字母等级,标准等级为 A、B、C、D 或 F:
num_grade = 85
if num_grade >= 90 | num_grade <= 100:
print("The student has a letter grade of A")
elif num_grade >= 80 | num_grade <= 89:
print("The student has a letter grade of B")
elif num_grade >= 70 | num_grade <= 79:
print("The student has a letter grade of C")
elif num_grade >= 60 | num_grade <= 69:
print("The student has a letter grade of D")
elif num_grade >= 0 | <= 59:
print("The student has a letter grade of F")
print (num_grade) # This confirms the actual value stored in num_grade.
print (type(num_grade)) # A test of the data type that should be stored in num_grade.
这是我希望从脚本中收到的示例输出,如键入的那样:
The student has a letter grade of B
85
<class 'int'>
Process finished with exit code 0
如果我在 num_grade 中存储整数 64 - 69 或 72 - 77,我得到的输出与上面第一个示例中显示的不同:
num_grade = 74 # Only the integer ranges 64 - 69 and 72 - 77 generate the "unexpected" output.
下面的输出:
74
<class 'int'>
Process finished with exit code 0
输出中省略了实际的“字母等级”字符串。我只看到 num_grade 中存储的值以及数据类型。我手动测试了所有值 0 - 100。
有人能解释导致此输出的原因吗?我的语法中似乎没有任何明显的错误,例如“if-elif”语句中的运算符不匹配。我有 运行 使用最新版本 PyCharm、Jupyter Notebook 和 Python 终端的脚本,并得到相同的结果。
我的电脑是 Windows 10 Professional version 2004 的较新型号的 PC。
如果您需要测试数字是否在范围内,您需要更改运算符并使用 and
,您使用的是二元条件运算符 or(|
).
以下是我认为您正在尝试创建的代码:
num_grade = 74
if num_grade >= 90 and num_grade <= 100:
# if the number is greater than or equal to 90 and less than or equal to 100 do this
# do not use the 'or' operator as the number,
# although not greater than or equal to 90, is less than or equal to 100
print("The student has a letter grade of A")
elif num_grade >= 80 and num_grade <= 89:
print("The student has a letter grade of B")
elif num_grade >= 70 and num_grade <= 79:
print("The student has a letter grade of C")
elif num_grade >= 60 and num_grade <= 69:
print("The student has a letter grade of D")
elif num_grade >= 0 and num_grade <= 59:
print("The student has a letter grade of F")
print (num_grade) # This confirms the actual value stored in num_grade.
print (type(num_grade)) # A test of t
我希望有人可以帮助解决我在使用 Python 脚本时遇到的意外输出问题。
我似乎无法让“if-elif”条件语句分别正确处理整数范围 64 - 69 和 72 - 77。
下面这个非常简单的脚本的目的是将学生的数字考试分数转换为匹配的字母等级,标准等级为 A、B、C、D 或 F:
num_grade = 85
if num_grade >= 90 | num_grade <= 100:
print("The student has a letter grade of A")
elif num_grade >= 80 | num_grade <= 89:
print("The student has a letter grade of B")
elif num_grade >= 70 | num_grade <= 79:
print("The student has a letter grade of C")
elif num_grade >= 60 | num_grade <= 69:
print("The student has a letter grade of D")
elif num_grade >= 0 | <= 59:
print("The student has a letter grade of F")
print (num_grade) # This confirms the actual value stored in num_grade.
print (type(num_grade)) # A test of the data type that should be stored in num_grade.
这是我希望从脚本中收到的示例输出,如键入的那样:
The student has a letter grade of B
85
<class 'int'>
Process finished with exit code 0
如果我在 num_grade 中存储整数 64 - 69 或 72 - 77,我得到的输出与上面第一个示例中显示的不同:
num_grade = 74 # Only the integer ranges 64 - 69 and 72 - 77 generate the "unexpected" output.
下面的输出:
74
<class 'int'>
Process finished with exit code 0
输出中省略了实际的“字母等级”字符串。我只看到 num_grade 中存储的值以及数据类型。我手动测试了所有值 0 - 100。
有人能解释导致此输出的原因吗?我的语法中似乎没有任何明显的错误,例如“if-elif”语句中的运算符不匹配。我有 运行 使用最新版本 PyCharm、Jupyter Notebook 和 Python 终端的脚本,并得到相同的结果。
我的电脑是 Windows 10 Professional version 2004 的较新型号的 PC。
如果您需要测试数字是否在范围内,您需要更改运算符并使用 and
,您使用的是二元条件运算符 or(|
).
以下是我认为您正在尝试创建的代码:
num_grade = 74
if num_grade >= 90 and num_grade <= 100:
# if the number is greater than or equal to 90 and less than or equal to 100 do this
# do not use the 'or' operator as the number,
# although not greater than or equal to 90, is less than or equal to 100
print("The student has a letter grade of A")
elif num_grade >= 80 and num_grade <= 89:
print("The student has a letter grade of B")
elif num_grade >= 70 and num_grade <= 79:
print("The student has a letter grade of C")
elif num_grade >= 60 and num_grade <= 69:
print("The student has a letter grade of D")
elif num_grade >= 0 and num_grade <= 59:
print("The student has a letter grade of F")
print (num_grade) # This confirms the actual value stored in num_grade.
print (type(num_grade)) # A test of t