TypeError: decoding str is not supported

TypeError: decoding str is not supported

我正在尝试为我的侄子棋盘游戏制作一个属性特征随机化器,并且我正在尝试将属性写入外部文件,以便他以后可以使用它们。当我尝试写入文件时出现错误

speedE = str('Speed -', str(speed))
TypeError: decoding str is not supported

我的代码正在将计算的属性添加到属性的名称中。 IE。 ('Strength - ',强度E) 我的代码是...

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()

我对写入外部文件还很陌生,而且不太顺利啊哈。 如果您能提供帮助,我和我的侄子将不胜感激,谢谢

不确定您希望str('Speed -', str(speed))做什么。

你想要的是一个字符串连接:

speedE2 = 'Speed -' + str(speed)
# replace other lines also

您也可以使用字符串格式化而不用担心类型转换:

speedE2 = 'Speed -{}'.format(speed)