Python Forum
How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu
#11
I would start with the module as-is and just add the code from my example. See how long it takes to import. Python is pretty quick about loading modules. I've never had a 1.1 MB module, but I'm sure I regularly import modules that add up to more than that. Don't worry about 80 columns. That is for readabiliy, and nobody is going to read this code.

You can place import commands anywhere in your python code but people expect them at the top of the module. It makes them easy to find.

You can import the module anywhere you want to use it. After the module is imported the first time, subsequent imports reuse the same module object.
Reply
#12
(Apr-11-2024, 03:35 AM)deanhystad Wrote: If you can't open a text file, convert the text file to a python module. This is how you can convert your bible verse text file to a python module.

bible_verse.py
from random import choice


# This is your bible verse text file converted into a multi-line string.
# All you have to do is put the text=""" before the first verse and """
# after the last verse.
text = """
This is a verse.
This is the second verse.

Here's another verse.
This is the last verse
"""

# This splits the multi-line string into a list of strings, one string per
# line (\n).  It throws away empty strings.
verses = [quote for quote in text.split("\n") if quote]


# This is a function you can use to get a random verse.
def random_verse():
    return choice(verses)
Use the module like this:

test.py
from bible_verse import random_verse


for i in range(10):
    print(random_verse())
I'm not surprised that nobody had an answer for how to fix a java security access problem for jython code running in a BBS. That is a small expert pool. I don't know how to fix that problem. Luckily the problem you need to fix was not that problem

Hello again, Dean. I was just trying to implement your suggestion when I ran into a new wall. I have everything set up, including a BibleVerse.py file, a command on the Main Menu of the game to access it, and an ANSI menu screen as well. Sadly -- and I kind of expected this to happen -- being as my BBS is running in a Mac Classic environment, apparently, that imposes the 32K size limit on text files, just as it does with SimpleText on Mac OS 9. As a result, the minute I tried to run the external/module on my BBS, I got a SyntaxError: "string constant too large (more than 32,767 characters)".

Based on that, I would have to break the file -- which is 740K in size -- into 25 individual files, and then somehow make that function you gave me randomly select one of those twenty-five files, and then randomly select a verse string in the randomly selected file that it chose.

Do you think that kind of a setup might work? And if so, exactly how would I edit the function you gave me so that it could do that?

Thanks for your time and help. I appreciate it. As I said earlier, no one else has been able to offer me any kind of a solution to the SecurityException.
Reply
#13
Do you really need the entire file? If you had 100 verses your users would never know the difference.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print text with big font and style tomtom 6 20,767 Aug-13-2024, 07:26 AM
Last Post: yazistilleriio
  Regex to catch what user has put in text box robertkwild 26 5,186 Jun-26-2024, 01:06 PM
Last Post: AdamHensley
  Cannot get cmd to print Python file Schauster 11 3,204 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  problem with print command in super() akbarza 5 1,839 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  pyaudio seems to randomly halt input. elpidiovaldez5 2 1,436 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 1,449 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Start print a text after open an async task via button Nietzsche 0 1,173 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 2,143 Jan-16-2023, 07:38 PM
Last Post: Skaperen
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,041 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  python sql query single quote in a string mg24 1 2,148 Nov-18-2022, 08:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020