我想知道如何将输出格式化为一个百分比,labScore=score(labPercent, labWeight)
在哪里?
这是我的样品:
def score(earnedPercent, overallWeight):
scorePercent = earnedPercent * overallWeight
scorePercent = scorePercent % 7
return scorePercent
def grade(gradePercent):
if gradePercent >=90 and gradePercent <= 100:
letterGrade="A"
elif gradePercent >=75 and gradePercent <= 90:
letterGrade="B"
elif gradePercent >= 60 and gradePercent <= 75:
letterGrade = "C"
elif gradePercent >= 50 and gradePercent <= 60:
letterGrade = "D"
elif gradePercent < 50:
letterGrade = "F"
return letterGrade
labWeight = 0.20
labPercent=int(input("What is your lab percent so far (-1 if no labs yet)?: "))
labScore=score(labPercent, labWeight)
assignmentWeight = 0.10
assignmentPercent=int(input("What is your assignment percent so far (-1 if no assignment yet?: "))
assignmentScore=score(assignmentPercent, assignmentWeight)
examWeight = 0.70
examPercent=int(input("What is your exam percent so far (-1 if no exams yet)?: "))
examScore=score(examPercent, examWeight)
答案 0 :(得分:1)
您需要将其打印出来,后跟文字%
字符,如图所示。注意,我也删除了函数版本中的scorePercent = scorePercent % 7
,因为不需要它。
import sys
def score(earnedPercent, overallWeight):
scorePercent = earnedPercent * overallWeight
return scorePercent
def grade(gradePercent):
if gradePercent >=90 and gradePercent <= 100:
letterGrade="A"
elif gradePercent >=75 and gradePercent <= 90:
letterGrade="B"
elif gradePercent >= 60 and gradePercent <= 75:
letterGrade = "C"
elif gradePercent >= 50 and gradePercent <= 60:
letterGrade = "D"
elif gradePercent < 50:
letterGrade = "F"
return letterGrade
labWeight = 0.20
labPercent=int(input("What is your lab percent so far (-1 if no labs yet)?: "))
labScore=score(labPercent, labWeight)
if labPercent > -1:
labScore=score(labPercent, labWeight)
print('labScore: {}%'.format(labScore))
sys.exit() # If you want to quit the program completely.
样品运行:
What is your lab percent so far (-1 if no labs yet)?: 69
labScore: 13.8%