为什么我的简单Python代码不起作用,我错过了什么?

huangapple go评论66阅读模式
英文:

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(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;)
print(&quot;Exam Grade Calculator&#128209;&quot;)
print(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;)
print()

#main
name_of_exam = input(&quot;What is the name of your exam?: &quot;)
max_score = int(input(&quot;What was the maximum score?: &quot;))
score = int(input(&quot;What was your score?: &quot;))
print()

result = score / max_score
percent = round(result * 100, 2)

if percent &gt;= 90:
  grade = &quot;A+&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

elif percent &gt;= 80 and percent &lt; 90:
  grade = &quot;A-&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

elif percent &gt;= 70 and percent &lt; 80:
  grade = &quot;B&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

elif percent &gt;= 60 and percent &lt; 70:
  grade = &quot;C&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

elif percent &gt;= 50 and percent &lt; 60:
  grade = &quot;D&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

else:
  grade = &quot;F&quot;
  print(&quot;You got&quot;, percent, &quot;which is a&quot;, grade)

#outro
print()
print(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;

-
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, &quot;.2f&quot;) + &quot;%&quot;. 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, &#39;A+&#39;),
    (80, &#39;A-&#39;),
    (70, &#39;B&#39;),
    (60, &#39;C&#39;),
    (50, &#39;D&#39;),
  ]
# intro
print(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;)
print(&quot;Exam Grade Calculator&#128209;&quot;)
print(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;)
print()

# main
name_of_exam = input(&quot;What is the name of your exam?: &quot;)
max_score = int(input(&quot;What was the maximum score?: &quot;))
score = int(input(&quot;What was your score?: &quot;))
print()

result = score / max_score
percent = round(result * 100, 2)

for p, g in GRADES:
    if percent &gt; p:
        grade = g
        break
else:
  grade = &#39;F&#39;

print(f&#39;You got {percent:.2f}% which is a {grade}&#39;)
# outro
print()
print(&quot;3[31m&quot; + &quot;-----------------------&quot; + &quot;3[0m&quot;)

答案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(&quot;What was your score?: &quot;))

and when printing the score, you could use this line instead of the one in your code:

  print(&quot;You got {}%, which is a {:.2f}&quot;.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.

huangapple
  • 本文由 发表于 2023年6月15日 20:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定