擅长:python、mysql、java
<p>用<code>reply in yesChoice</code>代替<code>reply == yesChoice</code><code>reply</code>是一个字符串,<code>yesChoice</code>是一个列表。您必须检查列表中是否有字符串</p>
<p>在while循环中不需要if语句。因为while循环每次运行时都会检查<code>reply in yesChoice</code>,如果<code>reply in yesChoice</code>是<code>false</code>,它就会退出</p>
<p>代码的正确版本:</p>
<pre><code>import requests
yesChoice = ['yes', 'y']
noChoice = ['no', 'n'] # variable not used
print('This is the Random Chuck Norris Joke Generator.\n')
reply=input("Would you like a joke?").lower()
while reply in yesChoice:
joke=requests.get('https://api.chucknorris.io/jokes/random')
data=joke.json()
print(data["value"])
reply=input("\nWould you like another joke?").lower()
print('Chuck Norris hopes you enjoyed his jokes.')
</code></pre>