0

I would like to make a class for taking inputs from the user and then return all these inputs for later process. An example, after taking inputs from the users (filenames) then the program stores the names in list. The program later will load the list and perform the process based on each filename for one process.

More explanations:

  1. User inputs 3 filenames, 3 output filenames, Name of item within file, Json filenames.
  2. I will create a class(Reason I create it so that it looks nice and wont be too messy since it doesnt contain in a function or class) to take these inputs and then return it.
  3. Program reads the input one by one and perform the process in one script.

My code:

class ReturnAllInput():
    def __init__(self,Morefilenames, Yourfilename, YourJsonName, Outputname, NameWithin):
        self.Morefilenames = Morefilenames
        self.Yourfilename = Yourfilename
        self.YourJsonName = YourJsonName
        self.Outputname = Outputname
        self.NameWithin = NameWithin

def allInput():

    Morefilenames = []
    while True:
        a = input("Please enter your Morefilenames " )
        if a == "Complete":
            break
        else:
            Morefilenames .append(a)
    # user typed complete
    print("You entered {} Morefilenames ".format(len(Morefilenames )))
    for n in Morefilenames :
       print('{}.txt'.format(n))

    Yourfilename= input("Yourfilename")
    YourJsonName= input("YourJsonName")
    Outputname= input("Outputname")
    NameWithin= input("NameWithin")

    return ReturnAllInput(Morefilenames , Yourfilename, YourJsonName, Outputname, NameWithin) 


for l in allinput(): #this is the section I do not know how to change it so that I could loop my return values of Morefilenames)
    if __name__ == "__main__":

If my codes arent good enough, please do let me know so that I could improve more. I am still a beginner and would like to learn more. Thank you in advance.

AChampion
  • 29,683
  • 4
  • 59
  • 75
JJson
  • 233
  • 1
  • 4
  • 18

1 Answers1

1

if __name__ == '__main__': is just used when this python file is being used as a script to control the execution, see What does if __name__ == "__main__": do? and would normally be the outer control logic, e.g.:

if __name__ == '__main__':
    for l in allinput():

allinput() returns the class, which isn't directly iterable. If you want to iterate over Morefilenames then reference that attribute, e.g.:

if __name__ == '__main__':
    for l in allinput().Morefilenames:
        print(l)

But you lose reference to you constructed class so it is probably better to separate these calls:

if __name__ == '__main__':
    user_inputs = allinput()
    for l in user_inputs.Morefilenames:
        print(l)
AChampion
  • 29,683
  • 4
  • 59
  • 75
  • Thank you so much for pointing out the mistakes I have. I will proceed to close this since it's not feasible. Sorry for posting the wrong one. – JJson Sep 03 '18 at 06:36