输入验证 - Python

时间:2017-11-30 05:10:50

标签: python python-2.7 validation

你好,这里是我的随机密码生成器的代码;

import string
import random
wt = input('Strength of the password: ')
while wt in (1,2,3):
    if wt == 1:
        pword = random.choice(['helloworld','environment','superman','textbook','sandwich','light','sparrow'])

    elif wt == 2:
        pword = ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(8))

    elif wt == 3:
        pword = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(8))

    print pword

    break

我没有成功验证wt的输入(尝试,除了块) 请帮我实现wt。

的输入验证

1 个答案:

答案 0 :(得分:0)

将输入转换为int: -

import string
import random
wt = int(input('Strength of the password: '))
while wt in (1,2,3):
    if wt == 1:
        pword = random.choice(['helloworld','environment','superman','textbook','sandwich','light','sparrow'])

    if wt == 2:
        pword = ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(8))

    if wt == 3:
        pword = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for i in range(8))

    print pword

    break