Why is my code coming up with the 'expected a character buffer object' python error when i try and write a file?
Question: I have been trying to write my attributes into a file like so:

    
    
    def player1():
            name1=raw_input("what would you like to call the first player?")
            diceroll()
            print name1,"has:"
            time.sleep(1)
            z = strength()
            time.sleep(1)
            x = skill()
            f = open('attributes.txt','w+')
            f.write(name1)
            f.write("has")
            f.write(x)
            f.write("strength and")
            f.write(z)
            f.write("skill")
    

However when I come to run my code it come up with:

    
    
    Traceback (most recent call last):
      File "H:\GCSE\Computing\Programming\shorterversion.py", line 83, in <module>
        loop()
      File "H:\GCSE\Computing\Programming\shorterversion.py", line 77, in loop
        player1()
      File "H:\GCSE\Computing\Programming\shorterversion.py", line 37, in player1
        f.write(x)
    TypeError: expected a character buffer object
    

How do I fix this? here is my entire code:

    
    
    import random
    import time
    def loop():
        global lol
        def intro():
            print "welcome to an attribute simulator game!"
            time.sleep(1)
            print "Today we will be calculating the strength and skill of virtual characters!"
    
        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 diceroll():
            print ("The dice of fate are being rolled!!")
    
        def player1():
            name1=raw_input("what would you like to call the first player?")
            diceroll()
            print name1,"has:"
            time.sleep(1)
            z = strength()
            time.sleep(1)
            x = skill()
            f = open('attributes.txt','w+')
            f.write(name1)
            f.write("has")
            f.write(x)
            f.write("strength and")
            f.write(z)
            f.write("skill")
    
        def player2():
            name2=raw_input("what would you like to call the first player?")
            diceroll()
            print name2,"has:"
            time.sleep(1)
            strength()
            time.sleep(1)
            skill()
            f = open('attributes.txt','w+')
            f.write(name2)
            f.write("has")
            f.write(x)
            f.write("strength and")
            f.write(z)
            f.write("skill")
    
    
        def player3():
            name3=raw_input("what would you like to call the first player?")
            diceroll()
            print name3,"has:"
            time.sleep(1)
            strength()
            time.sleep(1)
            skill()
            f = open('attributes.txt','w+')
            f.write(name3)
            f.write("has")
            f.write(x)
            f.write("strength and")
            f.write(z)
            f.write("skill")
    
        intro()
        time.sleep(1)
        player1()
        player2()
        player3()
        lol=raw_input("would you like to run the program again? Say yes or press any letter/number key to exit!")
    repeat = True
    while repeat==True:
        loop()
        time.sleep(1)
        if lol=="yes":
            print "ok lets start again!"
            repeat = True
        else:
            print "thankyou for playing!"
            time.sleep(1)
            print "exiting..."
            exit()
    


Answer: You need to think about the difference between printing something and
returning it. Your function here:

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

...doesn't explicitly return anything, which is the same as returning `None`.

The error you receive is exactly what you get if you attempt to write `None`
into a file:

    
    
    >>> f = open('test.txt', 'wt')
    >>> f.write(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected a character buffer object
    >>>
    

Change the skill() function to something like:

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

