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)
#1
It works fine if i give the right numbers LOL
All the input must be between 0 and 100, it doesn't check that.

How do i make the code simpler? For example, in if elif else part is there a way to just give range? I tried to do it but i couldn't.

Help me improve this code pls Angel

midterm_1=input('Please enter your first midterm score: ')
midterm_2=input('Please enter your second midterm score: ' )
midterm_total= float(midterm_1)*(20/100) + float(midterm_2) * (20/100)
print('your total percentage from midterm scores is: ' + str(midterm_total))

summary_paper=input('Please enter your summary paper score: ')
summary_total= float(summary_paper) * (10/100)
print('your total percentage from summary paper is: ' + str(summary_total))

response_paper=input('Please enter your response paper score: ')        
response_total= float(response_paper) * (20/100)
print('your total percentage from response paper is: ' + str(response_total))


i=1
my_list = []
while i <=10:
    try: 
        my_list.append(int(input('please enter your discussions scores: '))) 
        i += 1
    
    except: 
        print('you made an error,please try again')
print(my_list)


l=[]
for x in my_list: 
    dis_score = x * (2/100)
    l.append(dis_score)

print('your total percentage from discussion scores is: '+ str(sum(l)))


k=1
my_list2 = []
while k <= 5:
    try: 
        my_list2.append(int(input('please enter your top hat scores: '))) 
        k += 1
    
    except: 
        print('you made an error,please try again')
print(my_list2)

s=[]
for x in my_list2: 
    tophat_score = x * (2/100)
    s.append(tophat_score)

print('your total percentage from discussion scores is: '+ str(sum(s)))
    
total_sum = midterm_total + summary_total + response_total + sum(l)+ sum(s)

if total_sum >= 86:
    print('You received an A with total score of ' +str(total_sum))
    
elif total_sum>=74 and total_sum<=85 :
    print('You received a B with total score of ' +str(total_sum))

elif total_sum >=62 and total_sum<=73:    
    print('You received an C with total score of ' +str(total_sum))

elif total_sum >=54 and total_sum <=61:
    print('You received an D with total score of ' +str(total_sum))

else: 
    print('you failed the course')
Reply
#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
#3
Oh yes this is way better thanks! How about checking all the inputs is between 0 and 100 (also i have to make sure it must be only digits)It should be in a loop so if the user writes wrong it will print an error message and ask again
Reply
#4
Hi, you have hier two cycles on the same values:

i=1
my_list = []
while i <=10:
    try: 
        my_list.append(int(input('please enter your discussions scores: '))) 
        i += 1
     
    except: 
        print('you made an error,please try again')
print(my_list)
 
 
l=[]
for x in my_list: 
    dis_score = x * (2/100)
    l.append(dis_score)
 
print('your total percentage from discussion scores is: '+ str(sum(l)))
they can be combined into one:

my_list = []
l = []
for i in range(10):
    try:
        x = int(input('please enter your discussions scores: '))
        my_list.append(x)
        l.append(x * (2/100))
    except:
        print('you made an error,please try again')
        
print(my_list)
print('your total percentage from discussion scores is: '+ str(sum(l)))
The same thing can be done with the following two loops on my_list2 and s
Reply
#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


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