魔术8球程序循环不按预期工作

2024-10-03 02:37:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的班做这个魔术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:
       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)

我不确定我在这方面哪里出了很大的问题,因为我似乎无法让程序退出,一旦它在一般的响应函数,但任何指针将非常感谢。你知道吗

谢谢!你知道吗


Tags: orinyouinputyouriswhatare
1条回答
网友
1楼 · 发布于 2024-10-03 02:37:31

代码的问题是变量范围。在函数中定义变量时,它只在该函数中具有该值,除非使用“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)    

相关问题 更多 >