TypeError:不支持解码str [英] TypeError: decoding str is not supported
问题描述
我正在尝试为我的侄子棋盘游戏制作一个属性特征随机化器,并且试图将属性写入外部文件,以便他以后可以使用它们。当我尝试写入文件时,出现错误
Im trying to make a attribute characteristic randomiser for my nephews board game and I'm trying to write the attributes to an external file so that he can use them later. when i am trying to write to the file it comes up with the error
speedE = str('Speed -', str(speed))
TypeError: decoding str is not supported
我的代码正在添加计算出的属性属性名称。即('Strength-',strengthE)
我的代码是...
my code is adding the calculated attribute to the name of the attribute. I.E. ('Strength - ', strengthE) my code is ...
import random
char1 = open('Character1.txt', 'w')
strength = 10
strength += int(random.randint(1, 12) / random.randint(1,4))
speed = 10
speed += int(random.randint(1, 12) / random.randint(1,4))
speedE = str('Speed -', str(speed))
char1.write(speedE)
strengthE = str('Strength -', str(strength))
char1.write(strengthE)
print(char1)
char1.close()
char2 = open('Character2.txt', 'w')
strength2 = 10
strength2 += int(random.randint(1, 12) / random.randint(1,4))
speed2 = 10
speed += int(random.randint(1, 12) / random.randint(1,4))
speedE2 = str('Speed -', str(speed))
char2.write(speedE2)
strengthE2 = str('Strength -', str(strength))
char2.write(strengthE2)
print(char1)
char2.close()
im写入外部文件是很新的事情,而且操作不太方便。
我和我的侄子会很感激,如果可以的话,谢谢
im quite new to writing to external files and its not going too well aha. me and my nephew would really appreciate it if you could help, Thanks
推荐答案
不确定您的期望 str('Speed-',str(speed))
。
您想要的是字符串concat:
What you want is a string concat:
speedE2 = 'Speed -' + str(speed)
# replace other lines also
您也可以使用字符串格式,而不必担心类型转换:
You can also use string formatting and not worry about type casts:
speedE2 = 'Speed -{}'.format(speed)
这篇关于TypeError:不支持解码str的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!