You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Python代码中print("ok")语句无法执行?

问题分析:print("ok")无法执行的原因

你的print("ok")语句永远无法被执行,核心原因是上方的while True:是一个无限循环——这个循环没有设置任何终止条件,程序会一直卡在循环内部,重复执行输入密码、验证密码的逻辑,永远跳不到循环之后的代码行。

要解决这个问题,你需要在密码验证通过时添加终止循环的逻辑,让程序能跳出循环执行后续代码。比如修改代码如下:

def passvalidation(password):
    if len(password) < 8:
        print('password must be at least 8 characters')
        return False
    elif password.isnumeric():
        print("your password must contain at least one letter")
        return False
    elif password.isalpha():
        print("your password must contain at least one number")
        return False
    else:
        print("your password is correct")
        return True


while True:
    password = input("enter your password: ")
    # 验证通过时跳出循环
    if passvalidation(password):
        break


print("ok")

修改后,当输入的密码符合要求时,passvalidation函数会返回True,触发break语句跳出while循环,程序就能继续执行到print("ok")了。

内容的提问来源于stack exchange,提问作者Sajjad Kahani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:27:16