Python Forum
help me make this code better please (basic)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help me make this code better please (basic)
#2
Since most of the print is the same except for the letter, let's just set the letter.

Also, if you don't take the first branch, you know you can't be 86 or higher, so you don't have to test for it. Combining those two elements might give you this:

letter_grade = "Unknown"
if total_sum >= 86:
    letter_grade = "A"
elif total_sum>=74:
    letter_grade = "B"
elif total_sum >=62:
    letter_grade = "C"
elif total_sum >=54:
    letter_grade = "D"
else: 
    letter_grade = "F"

print(f"You received a {letter_grade} with a total score of {total_sum}")
This doesn't keep the "failed the course" sentence, but it would be easy to add that back with another if/then.

if total_sum >= 54:
    print(f"normal message about {letter_grade} and {total_score}")
else:
    print("You failed the course")
Reply


Messages In This Thread
RE: help me make this code better please (basic) - by bowlofred - Jun-04-2020, 03:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help to make the code smaller dexomol 2 3,051 Feb-26-2021, 09:56 PM
Last Post: dexomol

Forum Jump:

User Panel Messages

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