英文:
Why my simple python code isn't working, what am I missing?
问题
#intro
print("3[31m" + "-----------------------" + "3[0m")
print("Exam Grade Calculator📉")
print("3[31m" + "-----------------------" + "3[0m")
print()
#main
name_of_exam = input("What is the name of your exam?: ")
max_score = int(input("What was the maximum score?: "))
score = int(input("What was your score?: "))
print()
result = score / max_score
percent = round(result * 100, 2)
if percent >= 90:
grade = "A+"
print("You got", percent, "which is a", grade)
elif percent >= 80 and percent < 90:
grade = "A-"
print("You got", percent, "which is a", grade)
elif percent >= 70 and percent < 80:
grade = "B"
print("You got", percent, "which is a", grade)
elif percent >= 60 and percent < 70:
grade = "C"
print("You got", percent, "which is a", grade)
elif percent >= 50 and percent < 60:
grade = "D"
print("You got", percent, "which is a", grade)
else:
grade = "F"
print("You got", percent, "which is a", grade)
#outro
print()
print("3[31m" + "-----------------------" + "3[0m")
Hope this helps!
英文:
I started to learn python at replit a few weeks ago. The task is to write a program that will ask you to enter the name of your class, the maximum score possible, and your own score. Than print out how many % you got and your grade in float rounded at two decimals.
The code itself works fine. But I can't get the final result in % get to be rounded. Please help me finish it and point out where I was wrong in my code.
#intro
print("3[31m" + "-----------------------" + "3[0m")
print("Exam Grade Calculator📑")
print("3[31m" + "-----------------------" + "3[0m")
print()
#main
name_of_exam = input("What is the name of your exam?: ")
max_score = int(input("What was the maximum score?: "))
score = int(input("What was your score?: "))
print()
result = score / max_score
percent = round(result * 100, 2)
if percent >= 90:
grade = "A+"
print("You got", percent, "which is a", grade)
elif percent >= 80 and percent < 90:
grade = "A-"
print("You got", percent, "which is a", grade)
elif percent >= 70 and percent < 80:
grade = "B"
print("You got", percent, "which is a", grade)
elif percent >= 60 and percent < 70:
grade = "C"
print("You got", percent, "which is a", grade)
elif percent >= 50 and percent < 60:
grade = "D"
print("You got", percent, "which is a", grade)
else:
grade = "F"
print("You got", percent, "which is a", grade)
#outro
print()
print("3[31m" + "-----------------------" + "3[0m"
-
I've tried to change the calculation to be instead of this:
result = score / max_score
percent = round(result * 100, 2)
this:
result = score / max_score
percent = result * 100
round(percent, 2)
Than switch the round line (3rd) with percent calculation (2nd) just to see what it will do. Thank you for your help guys
答案1
得分: 4
使用 format(percent, ".2f") + "%"
. 你可以通过将 f
旁边的 2
替换为想要打印的小数位数。
英文:
Use format(percent, ".2f") + "%"
. You can specify as many as decimals you want to be printed by replacing 2
next to f
.
答案2
得分: 1
使用f-string来格式化输出百分比将是理想的。
此类情况也很适合使用"表驱动"的方式。类似于这样:
GRADES = [
(90, 'A+'),
(80, 'A-'),
(70, 'B'),
(60, 'C'),
(50, 'D'),
]
# intro
print("3[31m" + "-----------------------" + "3[0m")
print("Exam Grade Calculator🐙")
print("3[31m" + "-----------------------" + "3[0m")
print()
# main
name_of_exam = input("What is the name of your exam?: ")
max_score = int(input("What was the maximum score?: "))
score = int(input("What was your score?: "))
print()
result = score / max_score
percent = round(result * 100, 2)
for p, g in GRADES:
if percent > p:
grade = g
break
else:
grade = 'F'
print(f'You got {percent:.2f}% which is a {grade}')
# outro
print()
print("3[31m" + "-----------------------" + "3[0m")
英文:
Using an f-string to format the output percentage would be ideal.
Also, this kind of thing is well suited to being "table driven". Something like this:
GRADES = [
(90, 'A+'),
(80, 'A-'),
(70, 'B'),
(60, 'C'),
(50, 'D'),
]
# intro
print("3[31m" + "-----------------------" + "3[0m")
print("Exam Grade Calculator📑")
print("3[31m" + "-----------------------" + "3[0m")
print()
# main
name_of_exam = input("What is the name of your exam?: ")
max_score = int(input("What was the maximum score?: "))
score = int(input("What was your score?: "))
print()
result = score / max_score
percent = round(result * 100, 2)
for p, g in GRADES:
if percent > p:
grade = g
break
else:
grade = 'F'
print(f'You got {percent:.2f}% which is a {grade}')
# outro
print()
print("3[31m" + "-----------------------" + "3[0m")
答案3
得分: 0
不需要使用round函数。您需要做的是更改变量的打印方式,而不是触及变量本身。此外,在您的问题中,您说需要将实际成绩打印为浮点数,但在从用户输入中接收成绩后,您将其转换为“int”,这将删除小数点后的所有数字。因此,您应该使用以下行而不是您的代码中的行:
score = float(input("What was your score?: "))
在打印分数时,您可以使用以下行而不是您的代码中的行:
print("You got {}%, which is a {:.2f}".format(percent, score))
这将打印您的成绩四舍五入到两位小数点和精确的百分比,您没有指定它是否应该是浮点数还是整数(例如,28%与28.64%),但使用“:.2f”语句,您可以格式化百分比数字,使其四舍五入到两位小数点。祝好运。
英文:
You do not need to use the round function. What you need to do is to change the way the variable is printed, not touching the variable itself. Also, in your question, you say that you need to print your actual grade as a floating point, but upon receiving the grade from user input, you turn it into an "int" which will remove all of the numbers that come after the decimal point.. so you should use the line below instead of the one in your code:
score = float(input("What was your score?: "))
and when printing the score, you could use this line instead of the one in your code:
print("You got {}%, which is a {:.2f}".format(percent, score))
This will print your grade rounded up to two decimal points and the exact percentage, which you didn't specify if it should be a floating point number or an integer one (i.e. 28% vs 28.64%), but using the ":.2f" statement, you can format the percentage number so that it is rounded up to two decimal points to. too. good luck.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论