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
#5
So, to be clear, being as I already imported random, you are saying that I really don't need to add "from random import choice", being as "choice" is a part of the "random" module. Although it also sounds like you are saying to use "from random import choice" being as that is the only part that I need. BUT, if I do that, will it still randomly pick a quote each time that the loop is run, or will it do them in order, or what?

Before writing to you, I actually did add the import string, and I did place the function near the top of my main.py file.

However, as far as the rest of the code is concerned, I assumed that where your code says "with open('quote.txt', 'r') as quotes:" I am supposed to replace "quote.txt" with the actual name of the text file where all of the quotes are stored in my"strings" folder.

Sp I did that. Then I placed all of it -- minus the import call and the function -- together in the main entry part of my main.py file.

In other words, the "while True:" part is right below the "with open('quote.txt', 'r')" with the same indentation.

However, this jython installation that my BBS has does NOT like its placement, and keeps saying "Syntax Error: Invalid Syntax" with the little arrow right underneath the letter "o" in the word "open".

Quite often when I get that syntax error, it is because I have my indention wrong somewhere, or I forgot a comma, or a parenthesis. However, no matter how much, or how little, indentation I do, I still keep getting the syntax error.

Are you saying that instead of all of that other code you gave me, I can simplify things by using "random.choice(somelist or tuple)" instead, where I would replace "somelist or tuple" with the actual name of my text file? Do I need to put a print command below or above it?

I believe I may have tried something like that earlier today before I posted here, and I was unable to get it working properly.
Reply


Messages In This Thread
RE: How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu - by BillKochman - Apr-02-2024, 12:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot get cmd to print Python file Schauster 11 589 May-16-2024, 04:40 PM
Last Post: xMaxrayx
  problem with print command in super() akbarza 5 730 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  pyaudio seems to randomly halt input. elpidiovaldez5 2 482 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 723 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Start print a text after open an async task via button Nietzsche 0 765 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,213 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,211 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  python sql query single quote in a string mg24 1 1,161 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python run all py files from main py file mg24 6 1,494 Oct-12-2022, 04:41 AM
Last Post: mg24
  Saving the print result in a text file Calli 8 1,985 Sep-25-2022, 06:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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