String module object has no attribute join

2024/10/2 1:29:30

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function inside it and got an error:

Traceback (most recent call last):
File "C:\Users\Dennis\Tournament\inputbox.py", line 64, in <module>
if __name__ == '__main__': main()File "C:\Users\Dennis\Tournament\inputbox.py", line 62, in main
print(ask(screen, "Name") + " was entered")File "C:\Users\Dennis\Tournament\inputbox.py", line 46, in ask
display_box(screen, question + ": " + string.join(current_string,""))
AttributeError: 'module' object has no attribute 'join'

I tried running the inputbox.py while on it's own and got the same error. I am using Python 3.3 and Pygame 3.3 so that could be an issue. I was told that many 'string' functions have been removed lately. If anyone knows what the problem is and can fix it, then here is the code: I would be truly grateful if anyone can fix the problem as I've been trying to set up user inputs in my Pygame games for a long time now. Thanks a lot for the answers in advance.

# by Timothy Downs, inputbox written for my map editor# This program needs a little cleaning up
# It ignores the shift key
# And, for reasons of my own, this program converts "-" to "_"# A program to get user input, allowing backspace etc
# shown in a box in the middle of the screen
# Called by:
# import inputbox
# answer = inputbox.ask(screen, "Your name")
#
# Only near the center of the screen is blitted toimport pygame, pygame.font, pygame.event, pygame.draw, string
from pygame.locals import *def get_key():while 1:event = pygame.event.poll()if event.type == KEYDOWN:return event.keyelse:passdef display_box(screen, message):"Print a message in a box in the middle of the screen"fontobject = pygame.font.Font(None,18)pygame.draw.rect(screen, (0,0,0),((screen.get_width() / 2) - 100,(screen.get_height() / 2) - 10,200,20), 0)pygame.draw.rect(screen, (255,255,255),((screen.get_width() / 2) - 102,(screen.get_height() / 2) - 12,204,24), 1)if len(message) != 0:screen.blit(fontobject.render(message, 1, (255,255,255)),((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))pygame.display.flip()def ask(screen, question):"ask(screen, question) -> answer"pygame.font.init()current_string = []display_box(screen, question + ": " + string.join(current_string,""))while 1:inkey = get_key()if inkey == K_BACKSPACE:current_string = current_string[0:-1]elif inkey == K_RETURN:breakelif inkey == K_MINUS:current_string.append("_")elif inkey <= 127:current_string.append(chr(inkey))display_box(screen, question + ": " + string.join(current_string,""))return string.join(current_string,"")def main():screen = pygame.display.set_mode((320,240))print(ask(screen, "Name") + " was entered")if __name__ == '__main__': main()
Answer

You are trying to use the join method from the string module when you should be using it from the str object.

string.join(current_string,"")

that line for example should be

"".join(current_string)

where current_string is an iteratible.

Just a quick example on how the .join method works

", ".join(['a','b','c'])

will give you a str object of the letters a b and c separated by a comma and a space.

https://en.xdnf.cn/q/70899.html

Related Q&A

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…

Redirect while passing message in django

Im trying to run a redirect after I check to see if the user_settings exist for a user (if they dont exist - the user is taken to the form to input and save them).I want to redirect the user to the app…

Django sorting by date(day)

I want to sort models by day first and then by score, meaning Id like to see the the highest scoring Articles in each day. class Article(models.Model):date_modified = models.DateTimeField(blank=True, n…

ImportError: No module named pynotify. While the module is installed

So this error keeps coming back.Everytime I try to tun the script it returns saying:Traceback (most recent call last):File "cli.py", line 11, in <module>import pynotify ImportError: No …

business logic in Django

Id like to know where to put code that doesnt belong to a view, I mean, the logic.Ive been reading a few similar posts, but couldnt arrive to a conclusion. What I could understand is:A View is like a …

Faster alternatives to Pandas pivot_table

Im using Pandas pivot_table function on a large dataset (10 million rows, 6 columns). As execution time is paramount, I try to speed up the process. Currently it takes around 8 secs to process the whol…

How can I temporarily redirect the output of logging in Python?

Theres already a question that answers how to do this regarding sys.stdout and sys.stderr here: https://stackoverflow.com/a/14197079/198348 But that doesnt work everywhere. The logging module seems to …

trouble with creating a virtual environment in Windows 8, python 3.3

Im trying to create a virtual environment in Python, but I always get an error no matter how many times I re-install python-setuptools and pip. My computer is running Windows 8, and Im using Python 3.3…

Python imaplib search email with date and time

Im trying to read all emails from a particular date and time. mail = imaplib.IMAP4_SSL(self.url, self.port) mail.login(user, password) mail.select(self.folder) since = datetime.strftime(since, %d-%b-%Y…

cumsum() on multi-index pandas dataframe

I have a multi-index dataframe that shows the sum of transactions on a monthly frequency. I am trying to get a cumsum() on yearly basis that respects my mapid and service multi-index. However I dont kn…