0

So I made a class named Courses and now I want to create a variable that holdes this class depending on user input. For example:



varName = input("Enter name of Course")
class Course:
        def __init__(self , _Name, _ID, _digitsec, _studentnum, _capacity , _secnum):
            self.Name = _Name
            self.ID = _ID
            self.digitsec = _digitsec
            self.studentnum = _studentnum
            self.capacity = _capacity
            self.secnum = _secnum

now I want :

somevariablehere = Course(Name, ID, digitsec, studentnum, capacity, secnum)

and I want this variable to be called whatever the user said in varName input.

this is my full code:

while True:
    Name = input("Enter course name:")
    ID = input("Enter course ID:")
    capacity = input("Enter course capacity(Not per section):")
    secnum = input("Enter how many sections are possible:")
    studentnum = input("Enter number of students demanding:")
    digitsec = input("Enter how many digits each section should have(1 to 4):")
    class Course:
        def __init__(self , _Name, _ID, _digitsec, _studentnum, _capacity , _secnum):
            self.Name = _Name
            self.ID = _ID
            self.digitsec = _digitsec
            self.studentnum = _studentnum
            self.capacity = _capacity
            self.secnum = _secnum
    coursenameID = str(Name) + str(ID)
    somevariablehere = Course(Name, ID, digitsec, studentnum, capacity, secnum)

In this case I want somevariablehere to be equal to the "Name" the user specified in the "Name" input.

  • Welcome to Stack Overflow. "In this case I want somevariablehere to be equal to the "Name" the user specified in the "Name" input." - **no, you do not**. Please see the linked duplicate. Do not try to make separate variables. It does not work like how you want, and techniques for forcing it to work are error prone and much more complicated than doing it **properly**. The proper way is to make a list or a dictionary, and store the `Course` instances in that. Aside from that, yes, the definition for `Course` should be outside of the loop. – Karl Knechtel Apr 17 '23 at 19:03

0 Answers0