【发布时间】:2014-06-01 23:24:21
【问题描述】:
我创建了一段代码,将我的代码生成的一些数据保存到我电脑上的 txt 文件中,但是当我运行该程序时,我收到此错误,
Traceback (most recent call last):
File "E:\CA2 solution.py", line 14, in <module>
char1[1]="Strength now " +strh1
TypeError: Can't convert 'int' object to str implicitly
我不知道该怎么办,需要一些帮助来弄清楚,这是我的代码;
import random
char1=str(input('Please enter a name for character 1: '))
strh1=((random.randrange(1,4))//(random.randrange(1,12))+10)
skl1=((random.randrange(1,4))//(random.randrange(1,12))+10)
print ('%s has a strength value of %s and a skill value of %s)'%(char1,strh1,skl1))
char2=str(input('Please enter a name for character 2: '))
strh2=((random.randrange(1,4))//(random.randrange(1,12))+10)
skl2=((random.randrange(1,4))//(random.randrange(1,12))+10)
print('%s has a strength value of %s and a skill value of %s'%(char1,strh1,skl1))
char1[1]="Strength now " +strh1
char1[2]="Skill now " +skl1
char2[1]="Strength now " +strh2
print()
char2[2]="Skill now " +skl1
print()
myFile=open('CharAttValues.txt','wt')
for i in Char1:
myFile.write (i)
myFile.write ('\n')
for i in Char2:
myFile.write (i)
myFile.write('\n')
myFile.close()
我知道它与“str”定义有关,但我已经摆弄了大约 30 分钟(是的,我是一个新程序员},但仍然无济于事,所以如果有人可以为我提供一些帮助太好了,非常感谢
【问题讨论】:
标签: python string python-3.x save