# 魔术 8 球程序循环无法按预期工作

``````import random
import time

question = input('What is your question? ')

if 'Why?' in question or 'Why' in question or 'why' in question:
print('Why not?')
else:
randomResponse = random.randint(1,4)
if randomResponse == 1:
print('...the probabilities are in your favor...')
if randomResponse == 2:
print('...make no definite plans...')
if randomResponse == 3:
if randomResponse == 4:
``````

1) 按原样，代码只询问一个问题并提供答案。修改代码，使其包含一个循环，以不断提出问题并提供答案，直到用户不再有问题为止。

2) 程序在用户问题中寻找的唯一关键词是“为什么”。更改程序，使其至少检查另外三个关键字并提供特定于该关键字的答案。

3) 修改代码，以便在函数中确定通用答案，该函数的标头是 def generalResponse(question)：

``````import random
import time

question = input('What is your question?\nIf you are finished asking questions, type "Done".')

def generalResponse(question):
question = question
randomResponse = random.randint(1,4)
if question == "Done":
exit()
elif randomResponse == 1:
print('...the probabilities are in your favor...')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif randomResponse == 2:
print('...make no definite plans...')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif randomResponse == 3:
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif randomResponse == 4:
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')

while(question != "Done"):
if 'Why?' in question or 'Why' in question or 'why' in question:
print('Why not?')
question = input('What is next your question?\nIf you are finished asking questions, type "Done".')
elif 'How?' in question or 'How' in question or 'how' in question:
print('Leave it to the Universe to figure out how.')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif 'Who?' in question or 'Who' in question or 'who' in question:
print('Who are you?')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif 'Where?' in question or 'Where' in question or 'where'in question:
print('Sorry, I am not a GPS.')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
else:
generalResponse(question)
``````

``````import random
import time

question = input('What is your question?\nIf you are finished asking questions, type "Done".')

def generalResponse(question):
question = question
randomResponse = random.randint(1,4)
if question == "Done":
exit()
elif randomResponse == 1:
return '...the probabilities are in your favor...'
elif randomResponse == 2:
return '...make no definite plans...'
elif randomResponse == 3:
elif randomResponse == 4:

while(question != "Done"):
if 'Why?' in question or 'Why' in question or 'why' in question:
print('Why not?')
question = input('What is next your question?\nIf you are finished asking questions, type "Done".')
elif 'How?' in question or 'How' in question or 'how' in question:
print('Leave it to the Universe to figure out how.')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif 'Who?' in question or 'Who' in question or 'who' in question:
print('Who are you?')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif 'Where?' in question or 'Where' in question or 'where'in question:
print('Sorry, I am not a GPS.')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
else:
response = generalResponse(question)
print(response)
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
``````

``````import random
import time

# The convention is to use capital letters for constants.
QUESTION = 'What is your question?\nIf you are finished asking questions, type "Done".' # Put this text into a variable so that you only have to type it once.

GENERAL_RESPONSES = [
'...the probabilities are in your favor...',
'...make no definite plans...',
] # avoid clutter by keeping your data seperate from the program logic

def generalResponse(question):
randomResponse = random.randint(0,3) # we can just get the list index directly
return GENERAL_RESPONSES[randomResponse]

while True:
question = input(QUESTION)
if question == "Done":
exit()
elif 'Why?' in question or 'why'.upper() in question.upper(): # using .upper() let's you do a case insensitive search with one command.
print("Why not")
elif "How?" in question or "how".upper() in question.upper():
print('Leave it to the Universe to figure out how.')
elif 'Who?' in question or 'who'.upper() in question.upper():
print('Who are you?')
elif 'Where?' in question or 'where'.upper() in question.upper():
print('Sorry, I am not a GPS.')
else:
response = generalResponse(question)
print(response)
``````

0 条评论