Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何显示我的用户';什么是错误?_Python_Validation - Fatal编程技术网

Python 如何显示我的用户';什么是错误?

Python 如何显示我的用户';什么是错误?,python,validation,Python,Validation,我的用户想要创建一个密码,我应该检查它是否有效。到目前为止,我已经记下了代码来检查它是否有效。现在,下一步(确定它无效后)是告诉用户它无效,以及为什么他们的密码不是有效选项 while True: pw = input('Enter password to be tested if valid or not: ') correct_length = False uc_letter = False lc_letter = False no_blanks = True

我的用户想要创建一个密码,我应该检查它是否有效。到目前为止,我已经记下了代码来检查它是否有效。现在,下一步(确定它无效后)是告诉用户它无效,以及为什么他们的密码不是有效选项

while True:
   pw = input('Enter password to be tested if valid or not: ')
   correct_length = False
   uc_letter = False
   lc_letter = False
   no_blanks = True
   first_letter = False

   if len(pw) >= 8:
   correct_length = True

   for ch in pw:
      if ch.isupper():
         uc_letter = True

      if ch.islower():
         lc_letter = True

   if pw.isalnum():
      digit = True

   if pw[:1].isalpha():
      first_letter = True

   if not pw.find(' '):
      no_blanks = True

   if correct_length and uc_letter and lc_letter and digit and first_letter and no_blanks:
      valid_pw = True
      print('Your password to be tested is valid.')
   else:
      valid_pw = False
      print('Your password to be tested is not valid because:')
      print(----------)
      #This is the part where I'm suppose to display the errors if the user gets it wrong.
      #Initially, in the test for ch. above, I put in an else: with a print statement but because of the for- statement, it prints it out for every single character.

   answer = input('Try another password input? y/n ')
   if answer == 'y':
      answer = True
   else:
      break

检查所有有效条件。正确的方法是,不要像这样检查条件是否正确

if len(pw) >= 8:
   correct_length = True
查证

if len(pw) < 8:
   correct_length = False
   print "Password not lengthy"
如果len(pw)<8:
正确长度=错误
打印“密码不冗长”

这将有助于识别错误。基本上,找出所有计算结果都是假的,这样用户就可以指出这些错误。

Hm。。我认为您可以简单地放入额外的
else
语句,然后引发一个错误:

   if not pw.find(' '):
      no_blanks = True
   else:
      raise ValueError('Invalid input!')
和你的其他条件句一样

如果希望循环继续,可以打印消息,然后继续:

else:
    print("Invalid input! Please re enter it:")
    continue

希望这有帮助

只需检查哪个测试参数为false。更好的方法是将字典中的参数作为键,将true/false作为值;然后,提取虚假的数据就变得简单了。如果是Web应用程序,就应该考虑进行客户端验证和服务器端验证。客户端验证可以用JavaScript编写,服务器端验证可以用Python编写。谢谢!你的建议触动了我的大脑,我能够接受你的想法并找到一种展示它们的方式!我使用了一个额外的“else:”和一些额外的“if”语句;这是乏味的,占用了更多的线,但它的作品无论!再次感谢你,你给了我完成任务所需的提示:D