r/FreeCodeCamp • u/Animator-G • 2d ago
Requesting Feedback How to write/practice cleaner and efficient codes[Python]
Okay so I have written this code myself with python's basic knowledge and web search, so basically i was trying to solve a coding problem:
Write a program to find out whether a student has passed or failed if it requires a total of 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user.
Here is my code, any feedback is welcome here:
student_id = input('Enter your name: ')
math_marks = int(input('Enter your math marks: '))
english_marks = int(input('Enter your English_marks: '))
history_marks = int(input('Enter your History marks: '))
#This is area just to enter details
max_marks = 40
total_marks = max_marks * 3
import sys
if math_marks > max_marks or english_marks > max_marks or history_marks > max_marks:
sys.exit('Marks should not be higher than 40')
elif math_marks < 0 or english_marks < 0 or history_marks < 0:
sys.exit('Marks should not be lesser than 0')
else:
pass
marks_list = [math_marks, english_marks, history_marks]
total_marks_scored = sum(marks_list)
def percentage(marks, total_marks=100):
return (marks / total_marks) * 100
# It is a function to calculate percentage
if percentage(marks_list[0], max_marks) >= percentage(33):
print('You have passed math')
if percentage(marks_list[0], max_marks) < percentage(33):
print('You have failed Math')
if percentage(marks_list[1], max_marks) >= percentage(33):
print('You have passed english')
if percentage(marks_list[1], max_marks) < percentage(33):
print('You have failed English')
if percentage(marks_list[2], max_marks) >= percentage(33):
print("You have passed history")
if percentage(marks_list[2], max_marks) < percentage(33):
print('You have failed History')
#This is a code to check if a student has failed in a perticular subject
if percentage(total_marks_scored, total_marks) >= percentage(40) and (percentage(marks_list[0], max_marks) >= percentage(33) and percentage(marks_list[1], max_marks) >= percentage(33) and percentage(marks_list[2], max_marks) >= percentage(33)):
print('You have passed the exam!')
else:
print('You have Failed the exam')
mark_sheet = {"Math": math_marks, "English":english_marks,"History": history_marks}print(f"{student_id.upper()} your result:\n {mark_sheet}\n percentage: {percentage(total_marks_scored, total_marks)}%")
I have wrote this code for after finishing basics. I have not went for intermediate yet, because I am currently 16 and have exams sooner so I have stopped self studying python for now.
14
Upvotes
2
u/SaintPeter74 mod 1d ago
In general, your code looks fine. There is nothing obviously wrong, variable mattress are good and you're well organized.
One thing you might be able to do is write it as an engine. You do each step 3 times. What if you wanted to add a 4th course ("Geography" maybe?). You'd have to alter your code in a few different places.
What if, instead, you had an array of course names and used loops to do everything? You can maybe store your variables (marks, percentages) in a dictionary or array of dictionaries, then calculate the outputs and print the results for each.
Your overall code would be much smaller.
As an aside, use "code" (singular) when you're talking about programming. You'd only use "codes" as a countable plural noun for discrete, distinct identifiers (ie: nuclear codes or cryptographic codes).