How to turn a Python structural program into an object-oriented program

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

kors=[]

def print_menu():

    print ('\nGrade Processing Program')
    print ('1. Score input')
    print('2. Grades output')
    print ('3. Exit')
    print("-"*20)
    menu = int(input('select action(1~3) :')
    return menu

def input_score(kors):

    for i in range(3):
        while True:
            score = int(input('Korean'+str(i+1)+'Enter your grade')))
            if score < 0 or 100 < score:
                Please check the print ('Grade Input' value (0~100) and re-enter it\n')
            else:
                break
        kors.append(score)

def print_score(kors):

    sum=0
    avg=0.0
    for score in kors:
        sum += score
        print ('Korean', kors.index(score)+1,':', kors[kors.index(score)])
    avg = sum/len(kors)
    print('Total score:',sum)
    print ('Average :', avg)

def main():
    flag = True
    while(flag):
        menu = print_menu()    
        if menu < 1 or 3 < menu :
            print ('Check the scope of work and re-enter it (1 to 3)\n')
        elif menu == 1:    
            input_score(kors)
        elif menu == 2:
            print_score(kors)                   
        elif menu == 3:
            print ('Exit Program')
            flag = False

if __name__ == '__main__':
    main()

As above, how will it change if we organize the Korean grade program in the form of an object-oriented program? Please help me

oop program python

2022-09-20 08:45

1 Answers

If you vaguely ask how it will change and ask for future prediction, no one can answer it because it's God's domain, but if I were to make a joke about it, let's just make a general statement...

What is object-oriented programming? I don't know what it is, but it's programming with objects. What is an object? If you go to the Department of English and ask what an object is, you'll say an object, but in computer science, an object that can usually be initialized with two things, 'property' and 'behavior', is an object. They're called properties, or attributes, and methods.

Don't you think you've heard a lot about "dogs"? Every dog can have a breed, a gender, a name, and so on. I feel sorry for some reason), because you can do things like "shrieking" or "waking up" (but actually "waking up" is something that any animal can do), for example, PHP defines an object and uses it roughly as follows.

// Defines a class of dogs, a type of mammal.
class Dog extends Mammal
{
    // A newborn puppy doesn't have a name in particularly a name.
    // Only dogs and dog owners know what their names are.
    private $name;

    // When a person collects a dog as its owner, it is named then.
    public function getAdopted(string $name): void
    {
        if ($this->name === null) {
            $this->name = $name;
        }
    }

    // The dog does not disclose his name, but just confirms his name by answering when called.
    public function wakeUpByName(string $nameGuess): void
    {
        if ($name === $this->name) {
            $this->wakeUp();
        }
    }

    // A dog waking up makes no difference to a mammal waking up.
    // Here, wakeUp of the Mammal class is used as it is.
    // No one can force you to wake up, so protected
    protected function wakeUp(): void
    {
        parent::wakeUp();
    }
}

// Now that there is a 'general dog', it can be initialized as a specific dog.
$myDog = new Dog;
$myDog->wakeUpByName ('Go'); // Not awake
$myDog->getAdopted ("Go");
$myDog->wakeUpByName ('Go'); // Wake Up
$myDog->getAdopted ("Kongdol");
$myDog->wakeUpByName ('Kongdol'); // Not awake

Well, if you look at the code, the word is an object, but it's actually a subject! It has a life cycle, such as being born, living, acting, and so on in the world of code.

Now let's look at the code. I get a total of three Korean test scores for the code, so I make a total and an average. For the sequence of events going on here, what is the subject that has "temper" and "action"? If you don't decide that, you can't decide what an object is, so object-oriented programming can't be done dead or alive. So we have to figure it out. What is an object?What exactly is the nature and behavior of a given program in the procedure?

And that's the general rule. From here on out, depending on how you decide, "Okay! I've made up my mind, I'm going to define the object of ~~" here, the story enters a completely different phase. So I can't predict the future. I said it like a joke, but it's real. If you ask me to squeeze, I'll squeeze the object that asks the program user for the menu, and then I'll squeeze the object that the object will handle. I think I will, but that's not the answer either, and there are many hiking trails to achieve my goal.

I think this is the point where I can accompany you. From here on, the questioner has to climb. Good luck.


2022-09-20 08:45

If you have any answers or tips


© 2022 pinfo. All rights reserved.