在python中将输出显示为百分比
我正在设计一个简单的代码,从5个给定的测试中确定一个学生的分数为p(>65%)或NP。这是我迄今为止为它设计的代码,基于我的教授想要它的方式,我希望以百分比的形式显示total correct的结果,但我一直很难找到正确的编码方式在python中将输出显示为百分比,python,Python,我正在设计一个简单的代码,从5个给定的测试中确定一个学生的分数为p(>65%)或NP。这是我迄今为止为它设计的代码,基于我的教授想要它的方式,我希望以百分比的形式显示total correct的结果,但我一直很难找到正确的编码方式 # Initialize Variables studentName = "no name" test1 = 0 test2 = 0 test3 = 0 test4 = 0 test5 = 0 totalScore = 0 finalGrade = 0 gradeMe
# 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 )
要计算百分比,您需要除以最大总分,然后乘以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))
Isabelle,你需要知道每个测试中的最大可能点是什么(
test1
,test2
,test3
,test4
和test5
)。假设每个测试中的最大可能点数为100,可以稍微更改代码。您可以使用finalGrade=totalScore/5
,而不是finalGrade=totalScore/100*100.0
以下是完整的代码(带有上述更改)。=)
如果最大分数为100,则
totalScore
已经是所需的百分比。请查看or。另外,我看不出您在哪里输出finalGrade
totalScore/100*100
没有任何意义。你的意思是做(总分/5)*100
?如果你的问题是如何添加百分号亲爱的伊莎贝尔,我希望你发现下面给出的答案对你有帮助。如果您觉得答案有用,请接受其中一个答案。=)
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 )