可能重复:
终止Python脚本
我有一个简单的Python脚本,如果满足条件,我想停止执行.
例如:
done = True
if done:
# quit/stop/exit
else:
# do other stuff
Run Code Online (Sandbox Code Playgroud)
本质上,我正在寻找与函数体中的'return'关键字等效的东西,它允许代码流退出函数而不执行剩余的代码.
如何检查,如果用户的字符串输入是一个数字(例如-1,0,1等)?
user_input = input("Enter something:")
if type(user_input) == int:
print("Is a number")
else:
print("Not a number")
Run Code Online (Sandbox Code Playgroud)
由于input始终返回字符串,因此上述操作无效.
有没有一种方法可以用来检查a raw_input是否是整数?
我在网上研究后发现了这种方法:
print isinstance(raw_input("number: ")), int)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它并输入4例如,我得到FALSE.我是python的新手,任何帮助都会受到赞赏.