【发布时间】:2012-02-29 05:12:07
【问题描述】:
任务是让用户输入密码,然后使用递归确保密码中没有元音。如果是,则让用户重新输入密码。这是我到目前为止所拥有的:
def passwordCheck(pwd):
"""checks if pwd has any vowels in it."""#doc string
vowels = 'aeiou'#specifies the characters that aren't allowed
if pwd == '':
return 0
elif pwd == None:
return None#Shouldn't be necessary but just in case
elif pwd[0] not in vowels:#checks that the 1st(0th) character is not a vowel
return passwordCheck(pwd[1:])#gets rid of the 1st(0th) character and starts again
elif pwd[0] in vowels:#checks if the 1st(0th) character is a vowel
return 1#if it is, stops the function calls and returns a value
password = str(input('Please enter a password with no vowels in it: '))#asks user to input their new password
x = passwordCheck(password)#checks the password is valid, i.e. no vowels
while x == 1:#when the password entered contains a vowel
print('\nSorry, that is not a valid password.\nYour password cannot contain any vowels.')#tells the user why their password is invalid
password = str(input('\nPlease enter a different password: '))#gives the user a chance to re-enter their password
x = passwordCheck(password)#checks to make sure the new password is valid
print('\nCongratulations, you have entered a valid password!')#tells the user if their desired password is valid
print('\nYou are now able to log on to the system with these credentials.')#could've been included on the previous line but looks neater here
我知道这可能不是最 Pythonic 的方式,但在大多数情况下它对我有用。我很想听听更好的方法,但理想情况下有人可以以同样的方式提供帮助。我不想只是复制别人的代码而不理解它。
我的问题是处理用户根本不输入密码的情况。第一个 if 语句:
if pwd == '':
return 0
我认为它只是处理字符串完全递归的情况,即没有元音,但经过几分钟的检查,很明显这也适用于没有密码。 我也尝试过使用:
if pwd == None:
return something
现在我在想问题可能是因为我说:
password = str(input('######'))
但我也对此进行了修改,但似乎仍然无法让它发挥作用!我试过谷歌和搜索stackoverflow,但没有运气,所以如果有人有任何他们认为可能有帮助的想法/解决方案,我会非常感激听到他们的声音。非常感谢。
我的主要问题是:
我如何区分一个空字符串,因为它已经被递归了,而用户什么也没输入?
已解决。
最终使用
def passwordValid(pwd):
if len(pwd)>0 and passwordCheck(pwd)==0:
return pwd
else: return 'Fail'
password = str(input('Please enter a password with no vowels in it: '))#asks user to input their new password
y = passwordValid(password)#checks the password is valid, i.e. no vowels
while y == 'Fail':#when the password entered contains a vowel
print('\nSorry, that is not a valid password.\nYour password cannot contain any vowels or be empty.')#tells the user why their password is invalid
password = str(input('\nPlease enter a different password: '))#gives the user a chance to re-enter their password
y = passwordValid(password)#checks to make sure the new password is valid
print('\nCongratulations, you have entered a valid password!')#tells the user if their desired password is valid
print('\nYou are now able to log on to the system with these credentials.')#could've been included on the previous line but looks neater here
感谢 Wayne Werner 修正了标题和主要问题。
【问题讨论】:
-
这是一个可怕的递归作业问题。 (不是你的错,我知道;只是评论。)
-
但是,有趣的是,我最初使用/学习递归的方式(尽管它是实际验证,并不能确保它没有元音)。它使在以后的生活中使用递归变得更加容易。
-
@cheeken 明确为
check_password(pwd)可以定义为return not any(~pwd.find(vowel) for vowel in 'aeiou') -
@cheeken:在函数式语言中,您当然可以为此使用递归。恕我直言,虽然 Python 并不真正支持尾递归,但 Python 的功能“足够”使其相当现实,你当然应该在这里使用像
any这样的帮助器。 -
@NiklasB。许多学生在递归和理解何时以及为什么需要递归方面存在困难。像这样的问题,很容易用循环解决,我觉得只会进一步混淆他们的理解。
标签: python recursion python-3.x