python - 如何在 Python 中使用递归计数器?

标签 python recursion counter

我正在尝试用 Python 编写一个简单的“密码”程序,该程序允许使用递归函数尝试 3 次“登录”。我不明白为什么它不起作用......(是的,受侏罗纪公园启发)

def magicWord(count):
    int(count)
    answer = raw_input("What is the password? ")
    if answer == 'lucas':
        print 'ACESS GRANTED'
    else:
        count =+ 1
        if count > 2:
            while count > 2:
                count+=1
                print "na na na you didn\'t say the magic word. "
        else:
            magicWord(count)

magicWord(0)

最佳答案

你们很亲密。只有几个小的修复:

def magicWord(count):
    answer = raw_input("What is the password? ")
    if answer == 'lucas':
        print 'ACESS GRANTED'
    else:
        count += 1
        if count > 2:
            print "na na na you didn\'t say the magic word. "
            return
        else:
            magicWord(count)

这是一个示例 session :

>>> magicWord(0)
What is the password? alan
What is the password? ellie
What is the password? ian
na na na you didn't say the magic word.

关于python - 如何在 Python 中使用递归计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256675/

相关文章:

Python - 获取嵌套 JSON 中的列表

python - 如何使用 python 下载 nasa 卫星 OPeNDAP 数据

recursion - 尝试将 slice 作为参数传递给递归函数时 slice 超出范围 (Go)

javascript - 如何获取一个单词的所有可能集合 - JS

c++ - 在 C++ 中读取所有文件被 .文件夹

python - 使用次要术语(决胜局)在 python 中排序计数器集合

Python打印格式建议

python - 比较 Python 中的两个列表

Python Counter keys() 返回值

php - 使用 PHP 访问没有数据库的计数器