Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Nursery rhyme
- When called, displays a nursery rhyme
- These are stored in txt files in sub-folder "rhymes"
- '''
- from os import listdir
- from sys import exit
- def show_rhyme():
- try:
- files=listdir("rhymes") #sub folder of programs folder
- except:
- print("'rhymes' folder/files not found")
- sys.exit(0)
- for i, val in enumerate(files):
- print(i+1,val[0:-4]) #print filenames(minus .txt suffix) with an index
- while True:
- file=input("Pick a rhyme (1 to 5): ") #pick one
- if file=="q": #give up
- sys.exit(0)
- elif int(file)>=1 and int(file)<=5: #check if valid
- break
- else:
- print("no such rhyme, try again (or q to quit)") #go back around
- try:
- with open("rhymes//"+files[int(file)-1], 'r') as fh: #open the correct file
- content=fh.readlines() #read in the lines
- except:
- print("Error reading file")
- sys.exit(0)
- print()
- for lines in content:
- print(lines.strip()) #print rhyme
- #test
- show_rhyme()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement