0

「Python でプログラミングする方法を学ぶ」プロジェクトの一種として、単純な MUD を作成しています。永続的なものにしたいので、Python スクリプトを保存するための MySQL データベースがあります。ただし、現在、データベースから整数を取得して追加し、再度保存する作業に行き詰まっています。これが私のコードです:

initxp = cur.execute("SELECT Exp FROM CharactersDB WHERE ID=%s", name)
print "initxp is,", initxp
global xp
xp = int(initxp)
print "current xp is ", xp

global xpcounter
xpcounter = ("UPDATE CharactersDB SET Exp=%s WHERE ID=%s") #where is name variable


#save initial xp to db
cur.execute(xpcounter, (xp, name,))
db.commit()

#when you gain xp
def gainxp():
    global xp
    global xpcounter
    xp = xp + 10
    print name, "gains", xp, "xp!"
    #save new xp to db
    cur.execute(xpcounter, (xp, name,))
    db.commit()
    return xp


#save stats to the database,
#this funciton will be used in the character creation file

gainxp()

エラーはありませんが、いくつか奇妙なことが起こっています。

Firstly, when it prints the "initxp" variable, it returns with 1 every time. As does the "Current xp" line. So, when it enters into the gainxp() function, it just adds 1 to 10, saves the grand total of 11 and is on its merry way.

What we'd like to do is take that already saved 11 xp, add another 10 to it, save it, run it again and have another 10 added to it so we have 31xp, not a consistent 11 every time, which obviously defeats the purpose.

Thanks!

4

1 に答える 1