写入advi文件

2024-10-03 06:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码是:

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

我不想改变太多的代码(除非我真的不得不这样做),只需要解决这个问题。在


Tags: oftheisvaluerandommyfilewriteprint
3条回答

您只需从整型变量中生成一个字符串:

myFile.write("Strength: " + str(strch1) + "/n")

或者按照Alex的建议使用str.format()。在

错误消息确切地告诉您,strch1是一个int,不能直接将其与string连接起来。在

你能做到的

file.write('Strength: %s\n' % (sklch1))

使用字符串格式来消除错误:

file.write('Strength: {0}\n'.format(sklch1))

显然,当您使用sklch2写入文件时,您必须执行相同的操作。在

相关问题 更多 >