我想创建一个类来从用户那里获取输入,然后将所有这些输入返回以供以后处理。例如,从用户(文件名)获取输入后,程序会将名称存储在列表中。程序稍后将加载列表并基于一个进程的每个文件名执行该进程。
更多说明:
我的代码:
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__":
如果我的代码不够好,请告诉我,以便我可以进一步改善。我仍然是初学者,想了解更多。先感谢您。
答案 0 :(得分:1)
if __name__ == '__main__':
仅在此python文件用作控制执行的脚本时使用,请参见What does if __name__ == "__main__": do?,通常是外部控制逻辑,例如:
if __name__ == '__main__':
for l in allinput():
allinput()
返回无法直接迭代的类。如果您要遍历Morefilenames
,请引用该属性,例如:
if __name__ == '__main__':
for l in allinput().Morefilenames:
print(l)
但是您会丢失对构造类的引用,因此最好将这些调用分开:
if __name__ == '__main__':
user_inputs = allinput()
for l in user_inputs.Morefilenames:
print(l)