首页 > 解决方案 > 让输出在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 )

标签: python

解决方案


Isabelle,需要知道每个测试中的最大可能分是多少(test1test2test3和)。假设每个测试中的最大可能点为100,则可以稍微更改您的代码。而不是,您可以使用.test4test5finalGrade = totalScore / 100 * 100.0finalGrade = 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 ) 

推荐阅读