2017-07-16 83 views
-3

解决下面这样修复后的代码,以帮助任何人,如果他们遇到了这个见面...... 原来的问题 - 只有输入以外的整数过滤掉,而不是整数的范围1-10之外。条件仅在第一次迭代

lc = "abcdefghijklmnopqrstuvwxyz" #string for lc 
print("------------------------------------------------------------------------------------------------------------------------------------------------------") 

while True: 
    try: 
     passlen = int(input("How many lower case characters would you like in the password? (min 1, max 10) ")) 
     if 1<= passlen<=10: 
        a = "".join(random.sample(lc,passlen)) 
        print("") 


        counter = counter + (passlen) 
        left = 10 - counter 
        nleft = 16 - counter 
        if counter < 10: 
         print("You have used", counter, "characters. You need a minimum of", left, "characters and a maxium of", nleft, "characters to create a password of between 10 and 16 charcters.") 
         print("") 

        elif counter > 16: 
         print("You have used", counter, "charcters, more than the allowed maximum of 16 characters. Please follow the rest of the steps and retry when prompted.") 

        else: 
         print("You have used", counter, "characters, so have met the minimum amount of characters. You have a maximum of", nleft, "characters remaining to create a password with maximum 16 characters.") 
         print("") 

        break 

     else: 
      print("Input must be an integer between 1 and 10, please try again.") 
    except ValueError: 
      print("Input type must be an integer.") 
+0

完全不清楚这段代码应该做什么,再加上问题本身并不明显。 – tadman

+1

您能否提供您的问题[mcve]? –

+2

作为一个风格问题,“互动”这样的节目在20世纪80年代去世。编写一个命令行工具要比'sys.argv'参数好得多,比如你会有一个程序'python passgen.py 20',它会产生一个20个字符的密码或者如果参数产生错误被省略或超出范围。 – tadman

回答

1

的问题是,当你进入范围1-10之外的数字,它跳过while循环,你尝试处理错误,特别是while passlen <1 or passlen >10:

为了解决这个问题,手柄第一while循环内这个逻辑,远离第二个(省略长打印消息为了简洁)的。例如。

while True: 
    try: 
     passlen = int(input("How many lower case characters would you like in the password? (min 1, max 10) ")) 
     if 1 <= pathlen <= 10: 
      break 
     else: 
      print("Input must be an integer between 1 and 10, please try again.") 
    except ValueError: 
     print("Input type not recognized") 
+0

是的。像魅力一样工作,谢谢。从这里,我怎么把这个过滤的输入带到while循环之外(到它下面的代码)呢? – 13odobson

+0

您可以将所有代码放在您的最后一个else子句中。 – nico

+1

是的,在阅读你的回复之前就已经解决了这个问题 - (现在感觉有点傻)tbh重新命令了这样的反复,现在通过输入等改变了迭代感谢你的帮助,欣赏它,因为我是一个初学者...特别是因为问题不是很清楚(第一篇文章等)。无论如何程序运行完美。欢呼老板。 – 13odobson

相关问题