U
U
Umar Ilyasov2021-06-26 10:40:40
Python
Umar Ilyasov, 2021-06-26 10:40:40

How to properly implement data entry?

The question is banal, and perhaps stupid .... but how to Implement data input and output in Python OOP ...

I have some errors .... Eric Metiz's book does not say anything about this

Screen:

class Umar():
    def __init__(self, firstname, lastname, patronymic, age, birthday, jobs, car):
        self.firstname = firstname
        self.lastname = lastname
        self.patronymic = patronymic
        self.age = age
        self.birthday = birthday
        self.jobs = jobs
        self.car = car


    def print_info(self):
        full_info = "Имя: " + self.firstname + " Фамилия: " + self.lastname + " Отчество: " + self.patronymic + " Возраст: " + \
                    str(self.age) + " День рождения: " + self.birthday + " Работа: " + self.jobs + " Машина: " + self.car
        return full_info.title()

    def input_info(self, firstnames, lastnames, patronymics, ages, birthdays, jobss, cars):
        while True:
            print("Введите ваши данные или нажмите 'q' что-бы выйти из программы!")
            firstnames = input("Введите ваше имя")
            if firstnames == 'q':
                break
            lastnames = input("Введите вашу фамилию")
            if lastnames == 'q':
                break
            patronymics = input("Введите ваше отчество")
            if patronymics == 'q':
                break
            ages = str(input("Введите ваш возраст"))
            if ages == 'q':
                break
            birthdays = input("Ваша дата рождения")
            if birthdays == 'q':
                break
            jobss = input("Ваша работа")
            if jobss == 'q':
                break
            cars = input("Ваша машина")
            if cars == 'q':
                break



umar = Umar()
umar.input_info()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-26
@Vindicar

Well, first of all, what exactly are you trying to do?
> firstnames = input("Enter your name")
You are assigning the value entered by the user to a local variable of the input_info() method (more precisely, its parameter). Python does not provide for passing parameters by reference, so this assigned value will never leave the method, and will be lost when the method ends.
Further, why while True + break? What did not suit a simple return?
Finally, I would not introduce such methods into the class body. Let the main program deal with I/O the way it wants, the class that stores the data is not required to do this.

class Umar():
    def __init__(self, firstname, lastname, patronymic, age, birthday, jobs, car):
        ... # тут код инициализации класса
#эти функции можно сделать методами класса, 
# но я бы рекомендовал оставить их вне класса,
# так как способы ввода/вывода варьируются чаще, 
# чем способы хранения данных.
# красивый вывод содержимого класса
def pretty_print_umar(u: Umar):
    print('Имя:', u.firstname)
    ... # ну и так далее
# ввод данных с клавиатуры и создание по ним экземпляра класса
def input_umar() -> Umar:
    firstname = input('Введите имя [Enter - отмена]:')
    if not firstname:
        return
    # и так далее для остальных полей, а затем
    return Umar(firstname, ...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question