Magic 8 Ball程序循环无法按预期工作

时间:2018-09-09 22:59:07

标签: python python-3.x

我正在为课堂上的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)

我不确定这是哪里出了问题,因为一旦进入通用响应功能,我似乎无法使程序退出,但是任何指针将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:2)

您的代码的问题是可变范围。当您在函数中定义变量时,除非使用'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)