Python 3:如何打印验证字符串的程序

时间:2018-05-09 23:26:35

标签: python python-3.x

我试图用一个能够产生如下输出的主函数来完成我的代码:

consecutive('qqrrb12ss') = True
one_digit('qqrrb12ss') = True
not_nums('qqrrb12ss') = True
length('qqrrb12ss') =True
string_req('qqrrb12ss', old_strings) = True

consecutive('y123bz') = True
one_digit('y123bz') = True
not_nums('y123bz') = True
length('y123bz') = False
string_req('y123bz', old_strings) = False

consecutive('1222345') = False
one_digit('1222345') = True
not_nums('1222345') = False
length('1222345') = False
string_req('1222345', old_strings) = False

consecutive('top89hat') = True
one_digit('top89hat') = True
not_nums('top89hat') = True
length('top89hat') = True
string_req('top89hat', old_strings) = False

我的助手功能应该能够做到这一点:

  • 连续:如果字符串连续没有相同字符的3个子字符串,则返回True
  • one_digit:如果字符串至少有1位数,则返回True
  • not_nums:如果第一个和最后一个字符是字母,则返回True
  • length:字符串长度必须介于8和16之间才能返回True
  • string_req:除了确保以前没有输入新字符串外,它基本上是所有其他辅助函数中的一个

我所有的帮手功能(即连续的',' one_digit'等)都可以完美地完成。但是我很难将它们放在一起来创建这个输出。这是我到目前为止主要功能:

# Strings used to test function
old_strings = [ 'xyz556677abc', 'top89hat' ]
strings = [ 'qqrrb12ss', 'y123bz','1222345', 'top89hat' ]

def function():
    s = strings
    L = old_strings

    for # I do know I have to use a loop.. But how exactly should I use it?:
        print("consecutive", s, "=", consecutive) # should return True or False
        print("one_digit", s, "=", one_digit)
        print("not_nums", s, "=", not_nums)
        print("length", s, "=", length)
        print("string_req", s, L, "=", string_req)

此外,这是我得到的输出:

consecutive['aabb12cc', 'a123b', 'a1234546b', 'a1234546b0', 'abcdef1pqrstuvwx', 'abcdef1pqrstuvw', '1222345', 'bat23man']  --> <function consecutive at 0x101d9a950>
one_digit ['aabb12cc', 'a123b', 'a1234546b', 'a1234546b0', 'abcdef1pqrstuvwx', 'abcdef1pqrstuvw', '1222345', 'bat23man']  --> <function one_digit at 0x101d9a9d8>
not_nums ['aabb12cc', 'a123b', 'a1234546b', 'a1234546b0', 'abcdef1pqrstuvwx', 'abcdef1pqrstuvw', '1222345', 'bat23man']  --> <function not_nums at 0x101d9a8c8>
length ['aabb12cc', 'a123b', 'a1234546b', 'a1234546b0', 'abcdef1pqrstuvwx', 'abcdef1pqrstuvw', '1222345', 'bat23man']  --> <function length at 0x101d9a730>
string_req ['aabb12cc', 'a123b', 'a1234546b', 'a1234546b0', 'abcdef1pqrstuvwx', 'abcdef1pqrstuvw', '1222345', 'bat23man'] ['abc112233xyz', 'bat23man']  --> <function string_req at 0x101d9a6a8>

1 个答案:

答案 0 :(得分:2)

你可以这样使用它。

for old_s in old_string:
    print('consecutive {s} = {ans}'.format(s = old_s, ans = consecutive(old_s)))
    print('one_digit {s} = {ans}'.format(s = old_s, ans = one_digit(old_s)))
    print('not_nums {s} = {ans}'.format(s = old_s, ans = not_nums(old_s)))
    print('length {s} = {ans}'.format(s = old_s, ans = length(old_s)))
for new_s in strings:
    print('string_req of {s} and {L} = {ans}'.format(
           s = new_s, L = old_strings, ans = string_req(new_s, old_strings)))
相关问题