如果用戶輸入'否',程序將不會通過for循環。如果用戶輸入'no',我不希望它tmpfile.write(行)。如何結束while循環並跳過for循環?
def remove():
coname = raw_input('What company do you want to remove? ') # company name
f = open('codilist.txt')
tmpfile = open('codilist.tmp', 'w')
for line in f:
if coname.upper() in line:
while True:
answer = raw_input('Are you sure you want to remove ' + line.upper() + '?')
if answer == 'yes':
print line.upper() + '...has been removed.'
elif answer == 'no':
break # HERE IS WHERE I NEED HELP
else:
print 'Please choose yes or no.'
else:
tmpfile.write(line)
else:
print 'Company name is not listed.'
f.close()
tmpfile.close()
os.rename('codilist.tmp', 'codilist.txt')
這是GOTO合法的一種情況。 – 2012-07-08 07:11:06