擅长:python、mysql、java
<p>如果要在循环中循环,则应使用<code>continue</code>;如果要退出,则应使用<code>break</code>。在<code>while</code>循环中,您有一个<code>break</code>,因此它从不回圈。以下是我认为您需要的代码:</p>
<pre><code>quit_game = "Goodbye, thank you for playing."
tell_joke = "Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat?"
while True:
option = input('What to do? 1. Repeat 2. Quit: ')
if option.lower() == "repeat":
print(tell_joke)
continue
elif option.lower() == "quit":
print(quit_game)
break
else:
print ("Invalid option provided..provide the right one")
</code></pre>
<p>输出:</p>
<pre><code>What to do? 1. Repeat 2. Quit: bh
Invalid option provided..provide the right one
What to do? 1. Repeat 2. Quit: Repeat
Pete, Pete and Repeat went out on the lake in their boat. Pete and Pete fell out. Who is left in the boat?
What to do? 1. Repeat 2. Quit: Quit
Goodbye, thank you for playing.
</code></pre>