-2
class Student:
    def __init__(self, first, last, age, major):
        self.first = first 
        self.last = last
        self.age = age
        self.major = major
        self.courses = [] 
    
    def profile(self):
        print("Student name", self.first, ' ', self.last)
        print("Student age:", self.age)
        print(f"Major: {self.major}")
        
        
    def enrol(self, course):
        self.courses.append(course)
        print("enrolled ", self.first," in", course)
        
    
    def show_courses(self):
        print(f"{self.first + ''  + self.last} is taking the following courses")
        for course in self.courses:
            print(course)
        
                
s = Student('Sally', 'Harris', 20, 'Biology') # how do I get user input?
    
s.enrol('Biochemistry I')    

s.enrol('Literature')    

s.enrol('Mathematics')

s.show_courses()

Basically, I want to ask the user what the first, last, age, and major of 's' are.

I only know how to input that data in the parameters parenthesis. Is there any way I could code a user input line for that data?

Javad
  • 2,033
  • 3
  • 13
  • 23

2 Answers2

0

You can do this via input() method:

firstName = input("First name: ")
lastName = input("Last name: ")
age = int(input("Age: "))
major = input("Major: ")

s = Student(firstName, lastName, age, major)  

Notice how age is typecasted into an integer. This is because input() returns a value of the form of a string.

Javad
  • 2,033
  • 3
  • 13
  • 23
Lexpj
  • 921
  • 2
  • 6
  • 15
  • so I had to input the parameters as: s=Student (first="", last="", age="", major"") and it ran. Is that correct? – VincentOfTheHighlands Dec 17 '22 at 18:53
  • @VincentOfTheHighlands No you should pass the values you've taken from `input()` method. – Javad Dec 17 '22 at 18:55
  • @VincentOfTheHighlands it works, since you literally define the parameters to be "". No input is involved here. Student(first="",last="",age="",major="") is the same as Student("","","","") – Lexpj Dec 17 '22 at 18:58
  • but without a first="" in the parenthesis, the 'first' doesn't show up as a variable... – VincentOfTheHighlands Dec 17 '22 at 19:09
  • @VincentOfTheHighlands It's not necessary that you write your arguments names and if you pass the inputs based on the order of the arguments they will be passed correctly and without any problem. – Javad Dec 17 '22 at 19:23
  • but if I write the code as ---- s = Student(first, last, age, major) ----- it says first is not defined. I think I need to put first="" in order to make it a variable, albeit a blank variable, right? – VincentOfTheHighlands Dec 17 '22 at 19:30
  • @VincentOfTheHighlands No, Let me add more scripts to my answer that will help you. – Javad Dec 17 '22 at 19:37
-1

You can take and pass the input value for your Student instance using all the below instructions in your script:

s = Student(*(input().split(',')))
...
first, last, age, major = input().split(',')
s = Student(first=first, last=last, age=age, major=major)
...
first, last, age, major = input().split(',')
s = Student(first, last, age, major)
...
student_info = input().split(',')
s = Student(*student_info)
...

After running your script (with each set of the above instructions) what you enter in the terminal should be like the below pattern:

Sally,Harris,20,Biology

For example, the following image is an instance of the execution of this code:

Javad
  • 2,033
  • 3
  • 13
  • 23