Python if语句

时间:2014-04-22 17:56:19

标签: python

我是Python的新手

我有if: else声明。

我的问题:现在在打印“欢迎使用脚本”之后我的脚本退出了,但是我希望它在其他地方退出,并且当我的条件为真时继续行name=raw_input("Enter your name: ")等等不退出.. 。

如何退出if else并继续脚本?

script_username="test" 
script_password="1"        
login_username=raw_input('Please Enter the script username:')
login_password=getpass.getpass('Please Enter the script password:')

if login_password == script_password and login_username == script_username:
    print "Welcome to the Script" 

else:
    print "your Username or Password is false"
    exit()


name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")

2 个答案:

答案 0 :(得分:1)

试试(见新的缩进)

script_username="test" 
script_password="1"        
login_username=raw_input('Please Enter the script username:')
login_password=getpass.getpass('Please Enter the script password:')

if login_password == script_password and login_username == script_username:
    print "         Welcome to the Script           " 
else:
    print "     your Username or Password is false      "
    exit()

name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")

答案 1 :(得分:0)

我想我发现了这个问题。 getpass.getpass('prompt')不返回字符串。要么修复它,要么使用它:

script_username="test" 
script_password="1"        
login_username=raw_input('Please Enter the script username:')
login_password=raw_input('Please Enter the script password:')

if login_password == script_password and login_username == script_username:
    print "         Welcome to the Script           " 
else:
    print "     your Username or Password is false      "
    exit()

name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")

输出:

Please Enter the script username:test
Please Enter the script password:1
     Welcome to the Script           
Enter your name: Louis
Enter your family: Mom, Dad, Dog
Enter your Sex: Male
hello MaleLouisMom, Dad, Dog
press<enter>

重要

这个区块:

name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")

在您的问题中缩进错误。它应该在if语句中的东西下面(即没有空格)。这也会破坏你的程序(因为它认为所有内容都在if语句中。

编辑2:屏幕截图:

Screenshot