Reputation: 28
I just got my awnser to the question thank you to whoever awnsered it I really apriciate it! https://github.com/sick2as/New-Coding-Tutorial
Upvotes: 0
Views: 73
Reputation: 16081
I reversed your code,
from imghdr import tests
import random
import time
import json
print("Disclaimer this is a fake person with fake grades no real person had grades recorded ")
print("also thank you for using my software By: PTSCODING")
names = ["james", "john", "joe", "Jason", "Mick", "Nina"]
classes = ['math', 'science', 'social studies', 'reading', 'grammar']
rd_1 = random.randint(0, 100)
rd_2 = random.randint(70, 100)
student = {
"name": names[random.randint(0, 2)],
'tclass': [random.choice(classes)],
"assignment": [rd_1] + [rd_2]*2,
"test": [rd_2]*2,
"participation": [rd_1]*3
}
def get_time():
return time.localtime
def get_average(grade):
return float(sum(grade)) / len(grade)
def calculate_total_average(students):
assignment = get_average(students["assignment"])
test = get_average(students["test"])
return 0.1 * assignment + 0.7 * test + 0.2
def assign_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
def get_all_studinfo():
print(
f'Name: {student["name"]} \n Class {student["tclass"]} \n Assignments: {student["assignment"]} \n Tests: {student["test"]} \n'
)
print(
f"{student['name']}'s averages in every type of class is ",
round(calculate_total_average(student)), "\n"
)
print(
f"{student['name']}'s letter grade is ",
assign_letter_grade((calculate_total_average(student))), "\n"
)
print(
f"Now that you know {student['name']}'s grade you can now leave. \nHave a great day! :)")
js = {"Name": student['name'],
"grades": calculate_total_average(student)}
with open("student.json", 'w') as f:
f.write(json.dumps(js))
get_all_studinfo()
Things changed.
student
dictionary intialisationget_average
functionsjson.dumps
for the json file writing.Upvotes: 1
Reputation: 533
You can use a library called refrub
, it is a tool for refurbishing and modenizing Python codebases. And see what can be optimized.
See : https://github.com/dosisod/refurb
Or see this article on Medium : https://medium.com/@fareedkhandev/make-your-python-code-more-elegant-readable-or-modern-with-one-command-eb910cefded3
Upvotes: 1