为了好玩,我想创建一个python MADLIB。代码如下:
print "WELCOME TO MADLIBS! PY V1.2.1"
print ""
print "To see the story type all thirteen parts of speech below"
print ""
print "1. Foreign Country"
print "2. Adverb"
print "3. Adjective"
print "4. Animal"
print "5. Verb (ing)"
print "6. Verb"
print "7. Verb (ing)"
print "8. Adverb"
print "9. Adjective"
print "10. A Place"
print "11. Type of Liquid"
print "12. Part of the Body"
print "13. Verb"
print ""
while 13:
num=raw_input("Enter: ")
我想循环变量num
13次并以某种方式抓住第一个到第十三个输入并打印它们如下:
print "If you are traveling in" num "and find yourself having to cross a piranha-filled river, here's how to do it" num ". Piranhas are more" num "during the day, so cross the river at night. Avoid areas with netted" num "traps - piranhas may be" num "there looking to" num "them! When" num "the river, swim" num ". You don't want to wake them up and make them" num "! Whatever you do, if you have an open wound, try to find another way to get back to the" num ". Piranhas are attracted to fresh" num "and will most likely take a bite out of your" num "if you" num "in the water!"
如果您有更好的解决方案或我的固定版本,我很乐意听到它。
答案 0 :(得分:0)
您应该使用两件事,数组和字符串格式。这将使您的代码更清晰,更容易处理,除了它会使它更加pythonic。您的脚本的解决方案将是这样的:
print "WELCOME TO MADLIBS! PY V1.2.1"
print ""
print "To see the story type all thirteen parts of speech below"
print ""
print "1. Foreign Country"
print "2. Adverb"
print "3. Adjective"
print "4. Animal"
print "5. Verb (ing)"
print "6. Verb"
print "7. Verb (ing)"
print "8. Adverb"
print "9. Adjective"
print "10. A Place"
print "11. Type of Liquid"
print "12. Part of the Body"
print "13. Verb"
print ""
num = 0
answers = []
while num < 13:
val = raw_input("Enter %s: " num + 1)
answers.append(val)
num += 1
answer = """If you are traveling in %s and find yourself having to cross
a piranha-filled river, here's how to do it %s.
Piranhas are more %s during the day, so cross the river at night.
Avoid areas with netted %s traps - piranhas may be %s there looking to %s them!
When %s the river, swim %s. You don't want to wake them up and make them %s!
Whatever you do, if you have an open wound,
try to find another way to get back to the %s.
Piranhas are attracted to fresh %s and will most likely take a bite out
of your %s if you %s in the water!""" % (answers[0], answers[1], answers[2],
answers[3], answers[4], answers[5], answers[6], answers[7], answers[8],
answers[9], answers[10], answers[11], answers[12])
print answer
将值保存在数组中,允许您在其中包含多个值,然后将其格式化为长字符串,以便您更好地查看输出。
答案 1 :(得分:0)
试试这个版本:
input_types = ['Foreign Country', 'Adverb', 'Adjective', 'Animal', 'Verb (ing)']
input_types.append('Verb')
input_types.append('Verb (ing)')
input_types.append('Adverb')
input_types.append('Adjective')
input_types.append('A Place')
input_types.append('Type of Liquid')
input_types.append('Part of the Body')
input_types.append('Verb')
story = '''If you are traveling in {} and find yourself having to cross
a piranha-filled river, here's how to do it {}.
Piranhas are more {} during the day, so cross the river at night.
Avoid areas with netted {} traps - piranhas may be {} there looking to {} them!
When {} the river, swim {}. You don't want to wake them up and make them {}!
Whatever you do, if you have an open wound,
try to find another way to get back to the {}.
Piranhas are attracted to fresh {} and will most likely take a bite out
of your {} if you {} in the water!'''
print('WELCOME TO MADLIBS! PY V1.2.1\n')
print('To see the story type all thirteen parts of speech below')
results = []
for number, value in enumerate(input_types):
user_input = raw_input('#{} - {}: '.format(number+1, value))
results.append(user_input)
print('Here is your story: ')
print(story.format(*results))