我的代码是:
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()
错误如下:
^{pr2}$我不想改变太多的代码(除非我真的不得不这样做),只需要解决这个问题。在
您只需从整型变量中生成一个字符串:
myFile.write("Strength: " + str(strch1) + "/n")
或者按照Alex的建议使用
str.format()
。在错误消息确切地告诉您,
strch1
是一个int,不能直接将其与string连接起来。在你能做到的
使用字符串格式来消除错误:
显然,当您使用
sklch2
写入文件时,您必须执行相同的操作。在相关问题 更多 >
编程相关推荐