当我在Python 3中运行下面的代码(使用Mac终端)时,它给出了错误不支持的操作数类型+:' int'和' str' 。
number = int
bigger = int
print('Enter your favorite number: ')
number = int (input())
bigger = int(number) + 1
type1= type(number)
type2= type(bigger)
print (type1)
print (type2)
print( number + ' is pretty good, but ' + bigger + ' is better.')
为什么会发生错误,我该如何解决?
答案 0 :(得分:2)
问题是你要连接不同的类型。您可以将您的号码转换为字符串。它会像:
print( str(number) + ' is pretty good, but ' + str(bigger) + ' is better.')
或者可以使用格式化方法:
print( '{0} is pretty good, but {1} is better.'.format(number, bigger))
答案 1 :(得分:1)