Python Magic 8 Ball程序循环未按预期工作
我正在为我的班做这个魔法8球作业,我对作业的最后部分有一些问题 最初的代码是这样的:Python Magic 8 Ball程序循环未按预期工作,python,python-3.x,Python,Python 3.x,我正在为我的班做这个魔法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)
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:
print('...the answer is hazy...')
if randomResponse == 4:
print('...you already know the answer...')
任务基本上是这样做的:
1按原样,代码只询问一个问题并提供答案。修改代码,使其包含一个循环,以不断提问并提供答案,直到用户不再有问题为止
2程序在用户问题中寻找的唯一关键词是“为什么”。修改程序,使其检查至少三个以上的关键字,并提供特定于该关键字的答案
3修改代码,以便在函数中确定一般答案,函数的标题为def GENERALLRESPONSQUESTION:
我已经让1和2工作,但是3让我有点头疼,因为当我为我的一般响应创建一个单独的函数时,我似乎无法让我的程序脱离while循环
以下是我目前的代码:
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:
print('...the answer is hazy...')
question = input('What is your next question?\nIf you are finished asking questions, type "Done".')
elif randomResponse == 4:
print('...you already know the answer...')
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)
我不确定我在这方面哪里出了问题,因为我似乎无法让程序在通用响应函数中退出,但任何指针都将非常感谢
谢谢 代码的问题是可变范围。在函数中定义变量时,该变量仅在该函数中具有该值,除非使用“global”关键字将其设为全局变量。因此,当您进入“generalResponse”函数时,循环中“question”的值不会改变,因此循环会反复将您发送回“generalResponse”函数,并且无论您输入什么,程序都不会退出。要解决这个问题,最好让函数只确定响应,然后将其返回到循环中。通常,函数只能做一件事。它通常不应该获取输入、确定输出并打印。下面的代码将解决您的问题
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:
return '...the answer is hazy...'
elif randomResponse == 4:
return '...you already know the answer...'
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...',
'...the answer is hazy...',
'...you already know the answer...'
] # 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)
这里的问题是可变范围。函数中的'question'变量与循环中的'question'引用的内容不同。您要做的是从函数返回一个值,然后在循环中打印它。谢谢!你的回答真的很有帮助,让我明白了我的程序有什么问题。真的很感激!