In: Computer Science
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
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!