How would I make this more efficient? (python)

Question:

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

Answers:

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

Answered By: Adrien Riaux

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 intialisation
  • get_average functions
  • json.dumps for the json file writing.
Answered By: Rahul K P
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.