Question

In: Computer Science

language: python Create a text file in your project folder with at least 20 "quirky sayings"/fortunes...

language: python

Create a text file in your project folder with at least 20 "quirky sayings"/fortunes (the only requirement is that they be appropriate for display in class), If I use my own file though, you should handle as many fortunes as I put in. Make each fortune its own line,

•in your main function ask the user for the name of the fortunes file.•Create a function which takes the name of the fortunes file as a parameter, open that file, read all the lines into a list of strings Create a list by reading those 20+ fortunes from your file, then return that list from this functions•back in the main function call the display_fortunes passing the list of strings that you got from the previous functions•display_fortunes should•repeat as often as the user wants:

•ask the user if they want another fortune, •if the user’s answer begins with any variation in capitalization of the word ‘yes, then•select a random line from your list of fortunes and display it •if the users answer begins with no (in any capitalization) then quit the program

Solutions

Expert Solution

CODE:

import random
#function to read the file of fortunes
def returnFortuneList(filename):
#opening the file which can contain any number of lines
file = open(filename,'r')
#reading each line in the file and and storing it in the list
#file.readlines() returns a list of lines in the file
#but it contains character '\n' at the end of each line
#x.split('\n')[0] removes the '\n' if it is present in the line
fortuneList = [x.split('\n')[0] for x in file.readlines()]
#returning the list
return fortuneList

def display_fortunes(fortuneList):
choice = 'yes'
#while the user enter yes as a choice
while(choice.lower() == 'yes'):
print('\nFortune: ')
#prints a random fortune from the list
print(random.choice(fortuneList))
#asks the user whether to print again
choice = input('Do you want another fortune to be displayed? (yes/no): ')
print('Bye!')
#main method
def main():
#asking for the filename from the user
filename = input("Enter the filename: ")
#calling the first funciton
fortuneList = returnFortuneList(filename)
#calling the display function
display_fortunes(fortuneList)
#calling the main method
if(__name__ == '__main__'):
main()

_______________________________________

CODE IMAGES:

_____________________________________________

Input text file:

YOU CAN USE YOUR FORTUNE FILE, THE FORMAT WILL BE THE SAME AS IN THE TEXT FILE BELOW THAT IS ONE FORTUNE IN A LINE

Fortune Line 1
Fortune Line 2
Fortune Line 3
Fortune Line 4
Fortune Line 5
Fortune Line 6
Fortune Line 7
Fortune Line 8
Fortune Line 9
Fortune Line 10
Fortune Line 11
Fortune Line 12
Fortune Line 13
Fortune Line 14
Fortune Line 15
Fortune Line 16
Fortune Line 17
Fortune Line 18
Fortune Line 19
Fortune Line 20
Fortune Line 21
Fortune Line 22
Fortune Line 23
Fortune Line 24
Fortune Line 25
Fortune Line 26
Fortune Line 27
Fortune Line 28
Fortune Line 29
Fortune Line 30
Fortune Line 31
Fortune Line 32
Fortune Line 33
Fortune Line 34
Fortune Line 35
Fortune Line 36
Fortune Line 37
Fortune Line 38
Fortune Line 39
Fortune Line 40
_______________________________________________

OUTPUT:

____________________________________

Feel free to ask any questions in the comments section
Thank You!


Related Solutions

In the root of the project create a folder called res. In that folder create a...
In the root of the project create a folder called res. In that folder create a file called DemoFile.txt Create a class with a static main that tests the ability to resolve and print a Path: Create an instance of a FileSystem class. Create an instance of the Path interface for the DemoFile.txt file. Print the constructed Path with System.out.println() method. Create a class that does the following: Using a pre-Java 7 solution, create a class that tests streams in...
Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE:...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...
Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the...
C Language NO ARRAY UTILIZATION OR SORTING Create a .txt file with 20 integers in the range of 0 to 100. There may be repeats. The numbers must not be ordered/sorted. The task is to find and print the two smallest numbers. You must accomplish this task without sorting the file and without using arrays for any purpose. It is possible that the smallest numbers are repeated – you should print the number of occurrences of the two smallest numbers....
In Java language. Write a brief program that writes your name to a file in text...
In Java language. Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT