将结果转储到文件。 [蟒蛇]

时间:2013-11-11 21:07:03

标签: python json file attributes

我有点完成了我的老师设置的任务,即为输入其姓名的用户生成属性。我曾使用Json从列表中写入文件,但他说我们不允许导入除“数学”和“随机”之外的任何内容。我找不到任何其他方法将列表写入文件。有人可以帮助我,因为我真的被困住了。 他是代码。

import random
import math
import json

def attributes():
    return math.floor((random.randint(1,12)/random.randint(1,4))+10)

skill=[]
strength=[]
name=[]
result= {"Name" : name,"Strength" : strength,"Skill" : skill}

game=True

while game==True:
    nameinput = str(input('Enter your name. '))

    name.append(nameinput)
    skill.append(attributes())
    strength.append(attributes())
    print("Name:", name,"\nStrength", strength,"\nSkill", skill)

    g = open("Attributes.txt", "w")
    json.dump(result, g)
    g.close()

2 个答案:

答案 0 :(得分:0)

按照您的方式打开文件,然后使用str函数从result数组中创建一个字符串并将其写入文件。

答案 1 :(得分:0)

g = open("Attributes.txt", "a")
print >> g, result
g.close()
相关问题