addition with python into a .txt file

35 Views Asked by At

i am creating a chatroom bot using python 2.7.9 and i wanna know how to take the number i have in the .txt file and add a number to it, im stuck and i wanna know how to do this so i can add currency to my chatroom bot. ive tried to do this before but i am having too many problems with it. "args" is the argument/number i want to add "yp" is the currency i am using this is what i have, but it isnt working Definition for the currency:

yp = []
f = open("yp.txt", "r")
time.sleep(1)
for currency in f.readlines():
    if len(currency.strip())>0: yp.append(currency.strip())
f.close()

what i use to save the numbers to the file:

def saveyp(user):
f = open("yp.txt", "w")
f.write("\n".join(wl+args))
f.close()

and my command i have for the bot:

if used_prefix and cmd == "test" and user.name in owners:
    if args:
        yp.append(yp+args)
        saveyp(yp+args)
        room.message("$"+args+" has been added to your currency :)")
        room.message(user.name.capitalize()+", you now have $"+yp)
    else:
        room.message("ERROR!")
1

There are 1 best solutions below

0
On

I'm not sure exactly what you are trying to do or what errors you have been getting, but the number from the file will be encoded as a string, so you need to cast it as an integer before adding it to the currency. Maybe that's where you're getting an error?

Also, before room.message(user.name.capitalize()+", you now have $"+yp), you will need to increment currency somewhere so that you print the updated amount. You would do something like:

yp += int(args)

The way you have done it has not actually incremented the yp variable.