我想在询问用户时再次运行程序:"您是否想再次运行该程序"。不会退出,并且循环并再次运行main。但是我注意到python保存了旧的变量赋值。是否有一种快速清除内存的方法,以便在程序循环时再次存储新变量?
目前我刚刚得到:请关闭程序并重新打开它!想让它更优雅:
这是我到目前为止所得到的:
def main()
print("Do you want to solve another problem?")
answer = input()
while answer not in["yes","no"]:
answer = input()
if answer == "yes":
print("Please close the program and rerun it")
#main() #<-this is not working as expected
else:
exit()
答案 0 :(得分:0)
您可以做的一件事是本地化变量并具有清理功能来处理启动新会话。
def machstat():
dbi = mdb.connect("localhost", port=3306, user="access", passwd="password", db="database")
cursor = dbi.cursor()
cursor.execute("""SELECT operation FROM status WHERE mach1='down' """)
stat = cursor.fetchone()
for stat #THIS WHERE I NEED HELP
if stat == 'down' then root.after(2000, machstat)
else (AFTER PROCESS STOP)
for results in stat:
List.insert("end", results)
区别在于def main()
print("Do you want to solve another problem?")
answer = input()
while answer not in["yes","no"]:
answer = input()
if answer == "yes":
answer = None
print("Please close the program and rerun it")
#main() #<-this is not working as expected
else:
exit()
会将if answer == 'yes'
设置为清除它。