我正在为我的班级完成这项 Magic 8 Ball 作业,但我在作业的最后部分遇到了一些问题。
原来的代码是这样的:
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 generalResponse(question):
我已经让 #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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句