我有一个程序,用户输入数据,数据变成列表,然后main
函数循环遍历列表,直到它循环两次。然后程序应该返回maininput
,这样用户就可以输入更多数据。但是,我无法在maininput
之前启动main
,因此main
永远不会获得列表来启动整个过程。我尝试在程序开始时调用maininput,显然这不起作用,因为它还没有在第一行之前定义。
示例结构:
def maininput():
userdata = input('input data: ')
list=[]
#userdata turned into a list...
def main():
counter = 0
for input in list:
counter +=1
#stuff done with input
if counter == 2:
print('Program finished')
maininput()
else:
pass
while True:
main()
答案 0 :(得分:1)
我认为您需要一些参数来传递数据
def maininput():
userdata = input('input data: ')
#userdata turned into biglist1...
return biglist1
# take a list and the amount of iterations performed
def main(lst, counter=0):
if counter == 2: # change recursion when counter is reached
print('Program finished')
lst = maininput()
return main(lst) # also resetting the counter
for x in lst:
#stuff done with input
return main(lst, counter+1) # endlessly recurse
# No while loop needed, this function is recursive
main(maininput())
答案 1 :(得分:1)
如果程序必须无限期运行,并且如果使用大列表引用用户输入数据,则可以尝试:
<a href="mailto:a@mail.com?subject=<?=str_replace("&", "%26", the_title()); ?>">Apply</a><br> <!-- doesn't work -->
<a href="mailto:a@mail.com?subject=<?=str_replace("&", "%26", get_the_title()); ?>">Apply</a> <!-- works -->
答案 2 :(得分:0)
为什么不能在点击maininput()
循环之前调用while True
?
即:
maininput()
while True:
main()