Python Forum
Simple Method to calculate average and grade
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Method to calculate average and grade
#6
naughtyCat, your letter grade code does not work.:
average = [95]

for item in average:
    if item > 90: print(f"{item}: grade A")
    if item > 80: print(f"{item}: grade B")
    if item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
Output:
95: grade A 95: grade B 95: grade C
I thought the output looked odd and realized there were more grades reported in the output than there were grades in the score lists.

You could do this:
for item in average:
    if item > 90: print(f"{item}: grade A"); continue
    if item > 80: print(f"{item}: grade B"); continue
    if item > 70: print(f"{item}: grade C"); continue
    print(f"{item}: grade F")
But that having multiple statements on one line is frowned upon.

This is better, but still frowned upon.
for item in average:
    if item > 90: print(f"{item}: grade A")  # Still multiple statements on one line
    elif item > 80: print(f"{item}: grade B")
    elif item > 70: print(f"{item}: grade C")
    else: print(f"{item}: grade F")
Reply


Messages In This Thread
RE: Simple Method to calculate average and grade - by deanhystad - Aug-26-2021, 05:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Non Grade/School Help - PLEASE gbyrne12 8 4,280 Jun-19-2021, 07:31 PM
Last Post: snippsat
  How to calculate the lexical diversity average (with 1000 window word length) AOCL1234 6 4,822 Jul-27-2020, 06:16 AM
Last Post: DPaul
  Calculating Grade Average IstvanCH 5 7,053 Jan-27-2019, 04:42 PM
Last Post: aakashjha001
  Student grade program help debug ccm1776 3 8,005 Nov-14-2018, 02:41 AM
Last Post: stullis
  Grade Loop dtweaponx 8 14,351 Oct-17-2017, 02:01 PM
Last Post: buran
  Help? Letter Grade assignment.. zepel 3 5,447 Apr-23-2017, 12:47 PM
Last Post: idontreallywolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020