I am brand-new to Python, and this is my first post on DaniWeb. Seems like a positive community, and I hope one day to contribute as a knowledgable expert and not a world-class newbie.
I'm wanting to create a simple python program that "fakes" Shakespeare or haiku-style output. I realize actual poems are more a matter of syllables, but I'm just getting my toes wet.
I want to create a program that can take an example.txt file of plain text and output its contents in either 10-word lines or in 7-5-7 word lines; the number of words in a line is arbitrary.
I would do something like this, don't be shy of starting to use yield to generate answers instead of making one time use lists without purpose.
bookfile = '11.txt'
pattern = (7, 5, 7)
def nwords(book, nwords):
for dontcare in range(nwords):
## give word from begining (0) and remove from list of words one by one
yield book.pop(0)
## finish giving words after nwords has been given
with open(bookfile) as thebook:
# read text of book and split from white space
bookaslist = thebook.read().split()
# until bookaslist is empty, which is considered False value
while bookaslist:
for count in pattern:
# rejoin count words and center it for 60 column print
print ' '.join(nwords(bookaslist, count)).center(60)
# empty line between to clarify the form
print
Traceback (most recent call last):
File "/Users/jonmitten/Documents/workspace/LyndaPython3EssentialTraining/poemizer.py", line 24, in <module>
print( ' '.join(nwords(bookaslist, count)).center(60))
File "/Users/jonmitten/Documents/workspace/LyndaPython3EssentialTraining/poemizer.py", line 14, in nwords
yield book.pop(0)
IndexError: pop from empty list
from __future__ import print_function
bookfile = '11.txt'
pattern = (7, 5, 7)
def nwords(book, nwords):
for dontcare in range(nwords):
## give word from begining (0) and remove from list of words one by one
if book:
yield book.pop(0)
else:
break
## finish giving words after nwords has been given
with open(bookfile) as thebook:
# read text of book and split from white space
bookaslist = thebook.read().split()
# until bookaslist is empty, which is considered False value
while bookaslist:
for count in pattern:
# rejoin count words and center it for 60 column print
print(' '.join(nwords(bookaslist, count)).center(60))
# empty line between to clarify the form
print()
This means that it will return incorrect number of words from the end of the file, when the words finish at end of the file.
Yield is handy, but you can get sometimes confused with it. Main headaches is, that sometimes you think you are putting values to list, but you put generator of values or you forget that once generated the values of generator are lost, and finally that after generator finishes, it is empty and you must recreate it to start again.
Here is one infinite generator of numbers up from starting value. It can not be used in for loop without exit statement, because it has not exit condition. After each yield the state of function is saved and the execution continues forward to next result.
def countup(x):
while True:
yield x
x+=1
countfrom10= countup(10)
print(next(countfrom10))
print(next(countfrom10))
for i in range(10):
print(next(countfrom10))
print(next(countfrom10))