Lom*_*ore 8 python string decoding typeerror notsupportedexception
我试图为我的侄子棋盘游戏制作属性特征随机数,我试图将属性写入外部文件,以便以后可以使用它们.当我试图写入文件时,它会出现错误
speedE = str('Speed -', str(speed))
TypeError: decoding str is not supported
Run Code Online (Sandbox Code Playgroud)
我的代码是将计算出的属性添加到属性的名称中.IE('Strength - ',strengthE)我的代码是......
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()
Run Code Online (Sandbox Code Playgroud)
我写新的外部文件相当新,它不是太好了.如果你能提供帮助,我和我的侄子会非常感激,谢谢
不确定你期望str('Speed -', str(speed))
做什么.
你想要的是一个字符串concat:
speedE2 = 'Speed -' + str(speed)
# replace other lines also
Run Code Online (Sandbox Code Playgroud)
您还可以使用字符串格式,而不必担心类型转换:
speedE2 = 'Speed -{}'.format(speed)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29364 次 |
最近记录: |