Xei
Xei

Reputation: 21

Can't convert 'list' object to str implicitly python

elif used_prefix and cmd == "xp":
            if self.getAccess(user) >= 1:
                f = open("users/" + user.name.lower() + ".txt", 'r')
                word = f.readline().split("X Points = ")
                if word == "0":
                        room.message("You have no X Points")
                else:
                    room.message("You Have " + word + " X Points")
                f.close()
            else:
                room.message("You are not whitelisted " + user.name.capitalize())

When I try to use XP it shows Can't convert 'list' object to str implicitly as the error in the console. I'm using python 3.3.

Upvotes: 1

Views: 4122

Answers (2)

thefourtheye
thefourtheye

Reputation: 239683

You might need

word = f.readline().split("X Points = ")[1].strip()

as you are splitting, it will return the list of items split as a list. You need to take the element corresponding to the actual value

Example

data = "X Points = 10"
print data.split("X Points = ")

Output

['', '10']

So, we need to get the second element. Thats why we use [1]

Upvotes: 3

Burhan Khalid
Burhan Khalid

Reputation: 174748

The main issue is that split returns a list, not a string.

if self.getAccess(user) >= 1:
    with open("users/{}.txt".format(user.name.lower()), 'r') as f:
         word = f.readline().split("X Points = ")[1]
    if word == "0":
         room.message("You have no X Points")
    else:
         room.message("You Have {} X Points".format(word))
else:
    room.message("You are not whitelisted {}".format(user.name.capitalize()))

Upvotes: 0

Related Questions