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
#14
(Apr-01-2024, 09:53 PM)BillKochman Wrote: Hello. Just recently I put my BBS back online after nine years, and I am trying to write a python-based game -- for my BBS.

Before I continue, let me inform you that I am 70 years old, I am NOT a programmer, and I do not know any programming languages.

However, following the coding example of another open-source external that was written for my BBS, I have made some progress.

But, due my lack of expertise, I am stuck in several areas. Let me just discuss one of them in this current message.

I have a long list of quotes -- meaning several thousand of them -- in a text file, one quote per line. I have placed this text file in the "strings" folder for the python module that I am building for my BBS.

I have already imported both "random" and "string" at the top of my main.py file.

So, what I would like to do is create a function -- which I assume I must in some way declare at the top of my main.py file -- with an accompanying menu command, which I can add to the main menu in my python module.

I have done a lot of online research, and found a lot of different methods for doing what I want to do, which has left me rather confused.

Basically, once a user enters my game on my BBS -- meaning the python module -- I want them to be able to type a one-letter command that is shown on the main menu of the game module.

Typing this one-letter command will then have my game module randomly print one quote from the text file on the user's screen.

I don't know if this list of several thousand quotes would be referred to as a list, a dictionary, a set or what. As I said, I am very new to all of this, and being seventy, it is hard for my mind to grasp all of this stuff.

Being as this text file is one MB in size, I am assuming that it would not be wise to load it all into memory at once.

So, if anyone here has the patience to walk me through this step-by-step, explaining how to set this up, and how to link the command on the Main Menu, with clear examples of exactly what to type, and where to put it in my modules files, I would be most appreciative.

Thank you in advance.
With PDF Guru's Compress PDF tool https://pdfguru.com/compress-pdf reducing file sizes is a breeze. Effortlessly optimize PDF documents for faster sharing and easier storage. Meanwhile, the feature to randomly print a quote from a text file adds a delightful touch to your workflow. PDF Guru simplifies document management while injecting a bit of inspiration, making every task a pleasure to accomplish.

To randomly print a quote from a text file when a user types a command on the main menu, create a function in your main.py file. Use Python's random module to select a quote from the file and print it. Integrate this function into your main menu function, associating it with the command.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print command in super() akbarza 5 641 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  pyaudio seems to randomly halt input. elpidiovaldez5 2 410 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 656 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Start print a text after open an async task via button Nietzsche 0 727 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,122 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 1,150 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  python sql query single quote in a string mg24 1 1,095 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python run all py files from main py file mg24 6 1,386 Oct-12-2022, 04:41 AM
Last Post: mg24
  Saving the print result in a text file Calli 8 1,879 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,725 Apr-13-2022, 06:02 AM
Last Post: Paqqno

Forum Jump:

User Panel Messages

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