我正在学习python,我在这里遇到了一些新问题。任何人都可以解释这个python代码内部究竟发生了什么。
>>> s="sam"
>>> s +="dam"
>>> s
'samdam'
>>> d +=s
>>> d
'msamdam'
>>> f = f+s
Traceback (most recent call last):
File "<pyshell#129>", line 1, in <module>
f = f+s
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> f +=s
Traceback (most recent call last):
File "<pyshell#130>", line 1, in <module>
f +=s
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
答案 0 :(得分:1)
我可以假设变量f是整数类型而s是字符串。你不能用这种方式连接整数和字符串。如果你想这样做,它应该是:
str(f) + s
答案 1 :(得分:0)
f
是integer
,s
是string
。你不能连接数字和字符串。
你可以这样做:
x = str(f) + s
这会将f
转换为字符串,然后与s
连接。例如,如果f
为123
,x
将为123msamdam
。
答案 2 :(得分:0)
这里似乎f是一个整数,所以也许你可以使用:
key = getch()
while (True):
if (key == 'a'):
principal.adicionaragua()
principal.gastaragua()
principal.aumentardias()
principal.estado()
time.sleep(2)
clear()
这样,f就会变成一个字符串。