2 回答
TA贡献1809条经验 获得超8个赞
使用reply in yesChoice而不是reply == yesChoice。reply是一个字符串,yesChoice是一个列表。您必须检查字符串是否在列表中。
您不需要在 while 循环中使用 if 语句。reply in yesChoice因为 while 循环会在每次运行时检查,如果reply in yesChoice是false它就会退出。
您的代码的正确版本:
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.')
TA贡献1806条经验 获得超8个赞
等于运算符无法检查列表中的项目。要使此代码起作用,您需要将 yesChoice 和 noChoice 更改为字符串。如果您希望回复有选项,您需要更改您的 while 条件。
import requests
yesChoice = ['yes', 'y']
noChoice = ['no', 'n']
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()
if reply in noChoice:
print('Chuck Norris hopes you enjoyed his jokes.')
break
添加回答
举报