TypeError: decoding str is not supported

南楼画角 提交于 2019-12-22 04:06:28

问题


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

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


回答1:


Not sure about what you expect str('Speed -', str(speed)) to do.

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)


来源:https://stackoverflow.com/questions/40208812/typeerror-decoding-str-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!