0
XP = 0
nmin = 50
nmax = 2000

        getxp = []
        f = open("users/" + user.name.lower() + ".txt", 'r')
        xpg = f.readline().split("X Points = ",1)[1]
        f.close


elif used_prefix and cmd == "dailychance" or "dc":
                if self.getAccess(user) >=1:
                    ngnum = str(random.randrange(nmin,nmax))
                    getxp
                    XP += ngnum
                    room.message("Old XP = " + xpg[0])
                    xpg[1] = XP
                    room.message("New XP = " + xpg[1])
                    f.writeline(xpg[1])
                    XP = 0
                else:
                    room.message("You are not Whitelisted. >=( ")

I want it to get a random number between those min and max and then assign the number to ngnum then add ngnum to XP then send a message Old Xp = The old value then change the value to the new one then send the message New XP = new value then rewrite the old value to the new one.

4
  • 2
    xpg is a string (xpg = f.readline().split("X Points = ",1)[1]) and strings are immutable in Python - therefore it fails on xpg[1] = XP. Can you show us what is the input (the file) and expected output? Commented Dec 5, 2013 at 7:35
  • It would help if your code was syntactically correct (e.g. indentation) and could be run. Commented Dec 5, 2013 at 7:45
  • input file = <First line> X Points = 0 <Second Line> Nickname = Xei Commented Dec 5, 2013 at 7:53
  • @freakish Output file = X points = <old value + new value> how can i make it work? and can you suggest any good python tutotials? Commented Dec 5, 2013 at 11:07

1 Answer 1

1

@freakish has figured out the problem that causes your error. But there are other problems, too:

if cmd == "dailychance" or "dc":

doesn't do what you think it does. It's equivalent to

if (cmd == "dailychance") or "dc":

and will therefore always evaluate to a true value.

Use

if cmd in ("dailychance", "dc"):

instead.

Also, you need to call the .close method: f.close() (note the parentheses).

Sign up to request clarification or add additional context in comments.

5 Comments

That's not true: xpg[1] = XP while xpg = f.readline().split("X Points = ",1)[1] is a string. That's the cause.
@freakish: Ah, there we are. See, that's why I wrote "that I can see". You should write this as an answer.
No problem. I could write it as answer, however I would also like to know what exactly he is trying to achieve (i.e. input and output) so perhaps there's a better way to do it. The code is messy.
That's certainly true :)
trying to achieve output as New Xp = the value of xpg + the value of ngnum and it would write it in the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.