Dave
Dave

Reputation: 1

python: change class method to user input

This code I have gives the information of someone's name, gender, age, department, email, now I have to change it so the user can input their information instead of the info already there and I dont know how.

Anything will be usefull if you've seen a similar code that can help please comment thanks.

class Student:
    #name, gender, age, department, email, tuition
    def __init__(self, name, gender, age, department):
        self.name = name
        self.gender = gender
        self.age = age
        self.department = department
        self.email = f"{self.generate_account(name)}@example.com"

    def generate_account(self, name):
        fname = name.lower()[:name.index(" ")]
        lname = name.lower()[name.index(" ")+1:]
        return lname + fname

    def set_department(self, new_department):
        self.department = new_department

    def __str__(self):
        str = f"name = {self.name}\n"
        str = str + f"gender = {self.gender}\n"
        str = str + f"age = {self.age}\n"
        str = str + f"department = {self.department}\n"
        str = str + f"email = {self.email}\n"
        return str

student1 = Student("John Who", "Male", 19, "Social Science")
print(student1)

Result:

Name = John Who
Gender = Male
Age = 19
Department = Social Science
Email = [email protected]

Upvotes: 0

Views: 72

Answers (1)

zerecees
zerecees

Reputation: 699

Welcome to Stack!

Use the input() function! You can read more about it here.

Upvotes: 2

Related Questions