print 'You got' + str(score) + 'out of' + str(max_score)
print 'That\'s' + str(percentage) + '%'
即使我将类型更改为字符串,它仍然给我这个
Traceback (most recent call last):
File "maniasimplescoring.py", line 37, in <module>
print 'You got' + score + 'out of' + max_score
TypeError: cannot concatenate 'str' and 'float' objects
我做错了什么?
编辑:显然我的改变并没有节省。我在另一个编辑器中更改了它并且它工作了答案 0 :(得分:2)
使用格式,以提高可读性:
print 'You got {0} out of {1}'.format(score, max_score)
print "That's {0}%".format(percentage)
答案 1 :(得分:1)
这应该解决它:
print 'You got {} out of {}'.format(score, max_score)
print 'That\'s {}%'.format(percentage)