小编fol*_*tor的帖子

TypeError:/:'str'和'int'的不支持的操作数类型

在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 string int types typeerror

4
推荐指数
1
解决办法
3778
查看次数

SyntaxError在Python 3中没有例外

我正在尝试在我的程序顶部编写一个代码块,如果程序意外地在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)

我错过了什么?

python syntax-error python-2.7 python-3.x

1
推荐指数
1
解决办法
249
查看次数

标签 统计

python ×2

int ×1

python-2.7 ×1

python-3.x ×1

string ×1

syntax-error ×1

typeerror ×1

types ×1