Python:调用外部函数时的问题

2024-04-18 17:54:22 发布

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

我是Python的初学者,在函数方面遇到了问题。我正在做一个程序,其中我有许多参数供用户选择,我需要允许他们确认或拒绝他们的选择。下面是代表我的问题的简化代码部分。你知道吗

我的代码:

def confirm(function):
    while True:
        answer = raw_input('Are you sure? ')
        if answer == 'yes':
            break
        elif answer == 'no':
            return function() # if the user wants to change their name, recall function
        else:
            continue # to reprompt user if the answer is not "yes" or "no"

def your_name():
    while True:
        name = raw_input("What is your name? ")
        if not name:
            continue # to reprompt user if they do not enter anything
        else:
            confirm(your_name)
            print 'Congratulations! You have a name!'
            break
your_name()

运行此程序时,它将打印祝贺字符串,打印次数与answer收到输入的次数相同。
我的输出:

What is your name? Bastion
Are you sure? no
What is your name? Artex
Are you sure? no
What is your name? Falcor
Are you sure? yes
Congratulations! You have a name!
Congratulations! You have a name!
Congratulations! You have a name!

我的目的是只打印一次贺词。如何编辑我的函数来实现这一点?你知道吗

我尝试过的:
我已经尝试了所有这些,使用了与我在上面的输出块中使用的完全相同的输入值。 在confirm(function)的一节中,它说:

if answer == 'no':
    return function()

我试着把它改成:

if answer == 'no':
    function()

在输出中,这将要求answer原始输入3次,在每次输入之后发布祝贺消息。如果我这样写代码:

if answer == 'no':
    print function()

它将在下面的单独一行中打印祝贺回复3次。我正在寻找一个优雅,干净的格式,所以这不会做。你知道吗


Tags: no代码answernameyouyourifis
3条回答

所以你的问题是你正在创建一种递归函数,没有意义,你不需要传递函数来再次调用,因为你已经在函数中了。我建议如下:

def confirm():
    while True:
        answer = raw_input('Are you sure? ')
        if answer == 'yes':
            return True
        if answer == 'no':
            return False
        else:
            continue # to reprompt user if the answer is not "yes" or "no"

def your_name():
    while True:
        name = raw_input("What is your name? ")
        if not name:
            continue # to reprompt user if they do not enter anything
        elif confirm():
            print 'Congratulations! You have a name!'
            break
your_name()

由于您正在进行的样式递归(值得称赞),每次它们填充一个答案时,您都会调用your_name()函数一次。你知道吗

我想试试这样的方法:

def confirm():
    answer = ''
    while answer == '':
        answer = raw_input('Are you sure? ')
        if answer == 'yes':
            return True
        elif answer == 'no':
            return False
        else:
            answer = ''

def your_name():
    name = ''
    while name == '':
        name = raw_input("What is your name? ")
    if confirm():
        print 'Congratulations! You have a name!'
    else:
        your_name()

your_name()

我认为最干净的方法是将your_name改为:

def your_name(toplevel=False):
    while True:
        name = raw_input("What is your name? ")
        if not name:
            continue # to reprompt user if they do not enter anything
        else:
            confirm(your_name)
            if toplevel: print 'Congratulations! You have a name!'
            break

以及从顶层到your_name(True)的第一个调用。你知道吗

还有其他方法,但它们需要全局变量(ecch公司:-)或者更脏的把戏来找出函数是否是从顶层调用的;显式地告诉它是waycleaner。。。你知道吗

相关问题 更多 >