旨在循环的代码但是没有

时间:2019-04-18 18:15:41

标签: python

为什么这段代码没有循环?

while True:
  for i in text:
    print (ord(i))
    print (i , "=" , chr (ord(i) +n))
    password = (password + chr (ord(i) + n))

  if (text.lower() != text):
    print ("only lower case.")

  elif (n<2 or n>15):
    print ("your code must be between 2 and 15, including them.")

    return False

  else:
    print(text ,  "=>"  , password)

1 个答案:

答案 0 :(得分:0)

我不确定您的代码的初始输入或预期输出。但是仅从代码格式来看,“ if”语句不在While循环的范围内。请看:

while True:          
    for i in text:
        print (ord(i))
        print (i , "=" , chr (ord(i) +n))
        password = (password + chr (ord(i) + n))

if (text.lower() != text):
    print ("only lower case.")
elif (n<2 or n>15):
    print ("your code must be between 2 and 15, including them.")
    return False
else:
    print(text ,  "=>"  , password)

不同于:

while True:          
    for i in text:
        print (ord(i))
        print (i , "=" , chr (ord(i) +n))
        password = (password + chr (ord(i) + n))

    if (text.lower() != text):
        print ("only lower case.")
    elif (n<2 or n>15):
        print ("your code must be between 2 and 15, including them.")
        return False
    else:
        print(text ,  " => "  , password)

同样在您的'else'语句上,我假设您的While循环应该中断,因此我要添加'break'关键字。