在Python 2.7中:
a=80
b=100
def status(hp, maxhp):
print "You are at %r percent health." % hp*100/maxhp
status(a,b)
Run Code Online (Sandbox Code Playgroud)
返回:
TypeError:/:'str'和'int'的不支持的操作数类型
我已经尝试在每个变量和每个变量组合周围放置int().
我正在尝试在我的程序顶部编写一个代码块,如果程序意外地在Python 2.x中运行,它将给出一条错误消息并退出,但如果在Python中运行3.x将正常运行:
try:
print "Error: This program should only be run in Python 3."
raw_input('>')
exit()
except SyntaxError:
pass
print("I see you're running Python 3.")
# rest of program
Run Code Online (Sandbox Code Playgroud)
这在Python 2.7中正常工作(即,它显示错误和退出),但是当我在Python 3.3中运行它时,我得到一个SyntaxError,即使我告诉它有一个例外.
File "version.py", line 2
print "This program should only be run in Python 3"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我错过了什么?