在python中将输出显示为百分比

2024-04-23 09:16:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在设计一个简单的代码,从总共5个给定的测试中确定一个学生的成绩为p(>;65%)或NP。这是我迄今为止为它设计的代码,基于我的教授希望它的方式,我希望总正确的结果显示为一个百分比,但我一直有困难找到正确的方法来编码这个。你知道吗

# 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 )

Tags: testinputintscoreprintentertest1test2
2条回答

要计算百分比,需要除以最大总分,然后乘以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))

伊莎贝尔,你需要知道每个测试中的最大可能点是什么(test1test2test3test4test5)。假设每个测试中的最大可能点是100,可以稍微更改代码。您可以使用finalGrade = totalScore / 5,而不是finalGrade = totalScore / 100 * 100.0。你知道吗

下面是完整的代码(带有前面提到的更改)。=)

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 ) 

相关问题 更多 >