我只使用Python几个月而且我有点卡住了。这是一段较长的代码:
while True:
method=input('''How would you like to analyse your data?
1 = mean
2 = quartile
3 = mode
4 = range
5 = variance
6 = standard deviation
''')
if method == '1':
mean=sum(theList)/len(theList)
print('The mean of this data set is '+str(mean)+'.')
while True:
moveOn=input('Calculate another measure? Y/N ')
if moveOn == 'Y' or moveOn == 'y':
print('Redirecting...')
time.sleep(1)
break
elif moveOn == 'N' or moveOn == 'n':
print('Thank you for using the PDAP. ')
break
else:
print('Invalid response. ')
问题是,我需要elif选项来突破第一个while循环,但也需要淘汰第二个循环。如果有人输入'n',我需要编程才能完成并完全停止,但我似乎无法弄清楚如何做到这一点。
答案 0 :(得分:2)
你可以添加一个变量来打破循环,有点像这样:
_run = True
while _run:
while True:
[... do something ...]
_run = False
break
或者,如果您想退出程序,可以通过调用sys.exit()直接执行此操作。