擅长:python、mysql、java
<p>尝试:</p>
<pre><code>def yes_or_no(question):
reply = str(input(question+' (y/n): ')).lower().strip()
if reply[0] == 'y':
return 1
elif reply[0] == 'n':
return 0
else:
return yes_or_no("Please Enter (y/n) ")
print("started")
while True:
# DRAW PLOT HERE;
print("See plot....")
if(yes_or_no('Do you like the plot')):
break
print("done")
</code></pre>
<p>为了清晰起见,最好将函数定义与循环分开。另外,否则它将在每个循环中被读取,浪费资源。</p>
<p>输出:</p>
<pre><code>$ python ynquestion.py
started
See plot....
Do you like the plot (y/n): n
See plot....
Do you like the plot (y/n): N
See plot....
Do you like the plot (y/n): NO
See plot....
Do you like the plot (y/n): No
See plot....
Do you like the plot (y/n): no
See plot....
Do you like the plot (y/n): yes
done
$
</code></pre>