Python Basic if语句

时间:2016-04-10 17:03:45

标签: python

嗨大家为下面的代码我想只在第一个条件满足时执行所以请帮助我

print('welcome')
username = input()
if(username == "kevin"):
 print("Welcome"+username)
else:
 print("bye")
password = input()
if(password == "asdfg"):
 print("Acess Granted")
else:
 print("You Are not Kevin")

1 个答案:

答案 0 :(得分:0)

如果我做对了,你想在第一个if语句的“else”块中退出/终止脚本。

有两种方法 - 第一种是使用sys.exit():

import sys
#if statement here
...
else:
    print("bye")
    sys.exit()

第二个是你把你的第二个放在第一个if的else块之下:

...
else:
    password = input()
    if password=="abcdef":
        #do something here
    else:
        print("you're not Kevin")

另外,我看到你在if语句(if (condition):)之后使用了括号。就我而言,这对于Python 3.x来说并不是必需的。

请参阅:https://repl.it/CEl7/0

相关问题