这是我的代码:
import random
ch1=input("Please enter the name of your first character ")
strch1=(((random.randint(1,12)//(random.randint(1,4))))+10)
sklch1=(((random.randint(1,12)//(random.randint(1,4))))+10)
print("The strength value of "+ch1+" is:")
print (strch1)
print("and the skill value of "+ch1+" is:")
print (sklch1)
ch2=input("Please enter the name of your second character ")
strch2=(((random.randint(1,12)//(random.randint(1,4))))+10)
sklch2=(((random.randint(1,12)//(random.randint(1,4))))+10)
print("The strength value of "+ch2+" is:")
print (strch2)
print("and the skill value of "+ch2+" is:")
print (sklch2)
myFile = open("CharacterSkillAttributes.txt", "wt")
myFile.write("The attributes of "+ch1+" are: /n")
myFile.write("Strength: "+strch1+"/n")
myFile.write("Skill: "+sklch1+"/n")
myFile.write("The attributes of "+ch2+" are: /n")
myFile.write("Strength: "+strch2+"/n")
myFile.write("Skill: "+sklch2+"/n")
myFile.close()
这是错误:
Traceback (most recent call last):
  File "E:\Computing Science\Computing science A453\Task 2\task 2 mostly working (needs 'save to file').py", line 22, in <module>
    myFile.write("Strength: "+strch1+"/n")
TypeError: Can't convert 'int' object to str implicitly
我不想太多改变代码(除非我真的必须),只需要解决这个问题.
使用字符串格式来摆脱错误:
file.write('Strength: {0}\n'.format(sklch1))
显然,当你用文件写入文件时,你必须这样做sklch2.
| 归档时间: | 
 | 
| 查看次数: | 143 次 | 
| 最近记录: |