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)
#5
You can simplify all the input into one for statement using a dictionary.
import sys
gradeDict = {'midterm': {'number': 2, 'percent': 1/5}, 'summary paper': {'number': 1, 'percent': 1/10}, 'response paper': {'number': 1, 'percent': 1/5}, 'discussion score': {'number': 10, 'percent': 1/50}, 'top hat scores': {'number': 5, 'percent': 1/50}}

def formatNumber(num):
    formatDict = {'1': 'st', '2': 'nd', '3': 'rd'}
    lastNum = str(num)[len(str(num))-1:]
    try:
        return formatDict[lastNum]
    except:
        return 'th'

finalGrade = 0
for item, info in gradeDict.items():
    for count in range(1, info['number']+1):
        inputting = True
        while inputting:
            try:
                grade = float(input(f"Enter the grade for your {str(count)+formatNumber(count)+' '+item}: "))
                if grade < 0 or grade > 100:
                    print("Incorrect range for the grade")
                    continue
                finalGrade += grade*info['percent']
                inputting = False
            except:
                print("Invalid input")

percentReference = {86: 'A', 74: 'B', 62: 'C', 54: 'D', 0: 'F'}
for percent, grade in percentReference.items():
    if finalGrade >= percent:
        print(f"You got a(n) {grade} with an average of {finalGrade}")
        sys.exit()
If you have any questions on any of the code feel free to ask. Hope this helps!
Reply


Messages In This Thread
RE: help me make this code better please (basic) - by SheeppOSU - Jun-16-2020, 04:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help to make the code smaller dexomol 2 3,217 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