简体   繁体   中英

How do I write my generated information from python into a file?

I was wondering whether I could write the information of the attributes of each character into a text file and how would I be able to do this? The attributes on the notepad file need to be updated every time someone plays the game.

def strength():
    dice12 = random.randint(1,12)
    dice04 = random.randint(1,4)
    strfinalstats = dice12/dice04+10
    print ">",strfinalstats,"strength"

def skill():
    dice12 = random.randint(1,12)
    dice04 = random.randint(1,4)
    skfinalstats = dice12/dice04+10
    print ">",skfinalstats,"skill!"

def player1():
    name1=raw_input("what would you like to call the first player?")
    print name1,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

def player2():
    name2=raw_input("what would you like to call the first player?")
    print name2,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

def player3():
    name3=raw_input("what would you like to call the first player?")
    print name3,"has:"
    time.sleep(1)
    strength()
    time.sleep(1)
    skill()

First you need:``

f = open('filename', 'r+')

You can put right below each attribute:

f.write(attributes)

The attributes on the notepad file will update every time someone plays the game.

P/s: Why you do not declear a class name "player" then create three instance?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM