【发布时间】:2017-12-05 01:11:27
【问题描述】:
一个简单的 Madlibs 练习:
STORY = "This morning I woke up and felt %s because %s was going to finally %s over the big %s %s."
WORD_types = ('an adjective','a pronoun','a verb','an adjective','a noun')
WORD_values = []
for s in WORD_types:
print "Please give {}.".format(s)
s = raw_input()
WORD_values.append(s)
print STORY % tuple(WORD_values)
有没有办法用 .format 符号来完成最后一行?
STORY = "This morning I woke up and felt {} because {} was going to finally {} over the big {} {}."
WORD_types = ('an adjective','a pronoun','a verb','an adjective','a noun')
WORD_values = []
for s in WORD_types:
print "Please give {}.".format(s)
s = raw_input()
WORD_values.append(s)
print STORY.format(WORD_values)
这会返回以下错误:
Traceback (most recent call last):
File "Madlibs.py", line 12, in <module>
print STORY.format(WORD_values)
IndexError: tuple index out of range
【问题讨论】:
标签: python string list tuples string.format