gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:55:21 25 4
gpt4 key购买 nike

我正在尝试用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com