我正在设计简单的代码,从总共5次给定的测试中确定学生的成绩为P(> 65%)或NP。到目前为止,这是我为此设计的代码,基于我的教授的要求,我希望将完全正确的结果显示为百分比,但是我一直在寻找正确的代码编写方法。 / p>
# Initialize Variables
studentName = "no name"
test1 = 0
test2 = 0
test3 = 0
test4 = 0
test5 = 0
totalScore = 0
finalGrade = 0
gradeMessage = None
# Print report title
print("\n Isabelle S - Programming Problem Two")
# Get user input data
studentName = input("Enter name of student ")
test1 = int(input("Enter test score 1: "))
test2 = int(input("Enter test score 2: "))
test3 = int(input("Enter test score 3: "))
test4 = int(input("Enter test score 4: "))
test5 = int(input("Enter test score 5: "))
# Compute values
totalScore = test1 +test2 +test3 + test4 + test5
finalGrade = totalScore / 100 * 100.0
if finalGrade >65:
gradeMessage = "P"
else:
gradeMessage = "NP"
# Print detail lines
print("\n Name of student: " , studentName )
print("Total Correct: " , totalScore )
print("Final Grade: " , gradeMessage )
答案 0 :(得分:0)
要计算百分比,您需要除以最高总得分,然后将其乘以100得到百分比。
numberOfTests = 5
pointsPerTest = 100
finalGrade = totalScore/(numberOfTests*pointsPerTest) * 100
# Formatting like this allows all sorts of neat tricks, but for now
# it just lets us put that '%' sign after the number.
print("Final Grade percentage: {}%".format(finalGrade))
答案 1 :(得分:0)
伊莎贝尔(Isabelle),需要知道每次测试中的最大可能点数(test1
,test2
,test3
,test4
和test5
)。假设每个测试中的最高得分为 100 ,则可以稍微更改您的代码。可以使用finalGrade = totalScore / 100 * 100.0
代替finalGrade = totalScore / 5
。
以下为完整代码(具有上述更改)。 =)
from __future__ import division
# Initialize Variables
studentName = "no name"
test1 = 0
test2 = 0
test3 = 0
test4 = 0
test5 = 0
totalScore = 0
finalGrade = 0
gradeMessage = None
# Print report title
print("\n Isabelle Shankar - Programming Problem Two")
# Get user input data
studentName = input("Enter name of student ")
test1 = int(input("Enter test score 1: "))
test2 = int(input("Enter test score 2: "))
test3 = int(input("Enter test score 3: "))
test4 = int(input("Enter test score 4: "))
test5 = int(input("Enter test score 5: "))
# Compute values
totalScore = test1 +test2 +test3 + test4 + test5
finalGrade = totalScore / 5
print finalGrade
if finalGrade >65:
gradeMessage = "P"
else:
gradeMessage = "NP"
# Print detail lines
print("\n Name of student: " , studentName )
print("Total Correct: " , totalScore )
print("Final Grade: " , gradeMessage )