当前位置: 代码迷 >> python >> 如何在某些结果中删除打印命令
  详细解决方案

如何在某些结果中删除打印命令

热度:86   发布时间:2023-07-16 10:13:15.0

如何删除特定结果的命令:

pass1 = input("What is your password: ")
pass2 = input("Rewrite your password: ")

if len(pass1) > 5:
print ("")
else:
print ("Your password must be at least 5 characters long")

print ("*******************")
print ("Loading...")
print ("*******************")

time.sleep(1)

if pass1 == pass2:
    print ("All right, your password is: " + pass1)
else:
    print ("Sorry, your passwords don't match")

基本上,当我运行此命令并输入长度不超过5个字符的密码时,它仍然会显示我的密码。

我想做的是,当密码长度不超过5个字符时,我希望它不会显示出来。

if pass1 == pass2:
    print ("All right, your password is: " + pass1)
else:
    print ("Sorry, your passwords don't match")

只需在if分支内移动相关代码:

pass1 = input("What is your password: ")
pass2 = input("Rewrite your password: ")

if len(pass1) > 5:
    print ("")
    print ("*******************")
    print ("Loading...")
    print ("*******************")

    time.sleep(1)

    if pass1 == pass2:
        print ("Alright, you're password is: " + pass1)
    else:
        print ("Sorry, your passwords don't match")
else:
    print ("Your password must be at least 5 characters long")

您不能像这样嵌套if吗?

if len(pass1) > 5:
    if pass1 == pass2:
        #...
    else:
        #...
else:
    #...
  相关解决方案