Как запросить только те переменные, которые необходимы для истории в MadLib на Python

Прошу прощения, если на этот вопрос ответили в другом месте. Я новичок на 100%. Я прохожу свой путь через codeacademy, и одним из проектов был Madlib. С тех пор я передал это в визуальную студию и кое-что сделал сам с нуля. У меня была идея просто создать массивную базовую версию MadLib, чтобы все, что мне нужно было сделать, это написать историю с соответствующими необходимыми переменными. Когда я запускаю эту программу, она работает, но запрашивает каждый ввод. Как заставить программу запрашивать только те переменные, которые нужны мне для истории? Заранее спасибо, и вот код.

""" Newscast MadLib by Salem
"""

print ("Here comes a MadLib yall!!!")

noun1 = input("Enter a Noun: " )
noun2 = input("Enter a Noun: " )
noun3 = input("Enter a Noun: " )
noun4 = input("Enter a Noun: " )
noun5 = input("Enter a Noun: " )
adj1 = input("Enter an Adjective: ")
adj2 = input("Enter an Adjective: ")
adj3 = input("Enter an Adjective: ")
adj4 = input("Enter an Adjective: ") 
adj5 = input("Enter an Adjective: ")
verb1 = input("Enter a Verb: ")
verb2 = input("Enter a Verb: ")
verb3 = input("Enter a Verb: ")
verb4 = input("Enter a Verb: ")
verb5 = input("Enter a Verb: ")
adv1 = input("Enter a Adverb: ")
adv2 = input("Enter a Adverb: ")
adv3 = input("Enter a Adverb: ")
adv4 = input("Enter a Adverb: ")
adv5 = input("Enter a Adverb: ")
verbing1 = input("Enter a Verb ending in ing: ")
verbing2 = input("Enter a verb ending in ing: ")
verbing3 = input("Enter a verb ending in ing: ")
verbs1 = input("Enter a verb ending in S: ")
verbs2 = input("Enter a verb ending in S: ")
verbs3 = input("Enter a verb ending in S: ")
pnoun1 = input("Enter a Proper Noun: ")
pnoun2 = input("Enter a Proper Noun: ")
pnoun3 = input("Enter a Proper Noun: ")
pnoun4 = input("Enter a Proper Noun: ")
pnoun5 = input("Enter a Proper Noun: ")
plnoun1 = input("Enter a Plural Noun: ")
plnoun2 = input("Enter a Plural Noun: ") 
plnoun3 = input("Enter a Plural Noun: ")
plnoun4 = input("Enter a Plural Noun: ")
plnoun5 = input("Enter a Plural Noun: ")
body1 = input("Enter a Body Part: ")
body2 = input("Enter a Body Part: ")
body3 = input("Enter a Body Part: ")
body4 = input("Enter a Body Part: ")
body5 = input("Enter a Body Part: ")
bodypl1 = input("Enter a part of the body Plural: ")
bodypl2 = input("Enter a part of the body Plural: ")
bodypl3 = input("Enter a part of the body Plural: ")
emotion1 = input("Enter an Emotion: ")
emotion2 = input("Enter an Emotion: ")
emotion3 = input("Enter an Emotion: ")
historicm1 = input("Enter a Historic Monument: ")
historicm2 = input("Enter a Historic Monument: ")
mansname1 = input("Enter a Mans Name: ")
mansname2 = input("Enter a Mans Name: ")
womname1 = input("Enter a Womans Name: ")
womname2 = input("Enter a Womans Name: ")
occup1 = input("Enter an Occupation: ")
occup2 = input("Enter an Occupation: ")
occup3 = input("Enter an Occupation: ")
shape1 = input("Enter a Shape: ")
shape2 = input("Enter a Shape: ")
shape3 = input("Enter a Shape: ")
rest1 = input("Enter a Restaurant Name: ")
rest2 = input("Enter a Restaurant Name: ")
rest4 = input("Enter a Restaurant Name: ")
liquid1 = input("Enter a type of Liquid: ")
liquid2 = input("Enter a type of Liquid: ")
personir1 = input("Enter the name of someone in the room: ")
personir2 = input("Enter the name of someone in the room: ")
personir4 = input("Enter the name of someone in the room: ")
food1 = input("Enter a type of Food: ")
food2 = input("Enter a type of Food: ")
food3 = input("Enter a type of Food: ")
fperson1 = input("Enter the name of a famous person: ")
fperson2 = input("Enter the name of a famous person: ")
fperson3 = input("Enter the name of a famous person: ")
sword1 = input("Enter a Silly Word: ")
sword2 = input("Enter a Silly Word: ")
sword3 = input("Enter a Silly Word: ")
num1 = input("Enter a Number: ")
num2 = input("Enter a Number: ")
num3 = input("Enter a Number: ")
num4 = input("Enter a Number: ")
num5 = input("Enter a Number: ")
place1 = input("Enter a Place: ")
place2 = input("Enter a Place: ")
place3 = input("Enter a Place: ")
place4 = input("Enter a Place: ")
place5 = input("Enter a Place: ")


story = ("Putting on a newscast might look easy, but it takes a lot of %s work. Go behind the scenes, and you will see dozens of %s workers %s in every direction! Reporters run back and forth between the studio and locations all around %s to cover %s stories and interview %s . They are joined by videographers who operate handheld %s to capture all the action. The anchors are the people who %s behind the news desk and read the stories during the newsccast. They have to look %s on air, so they can often be found getting makeup applied to their %s . The director tells everyone where and when to %s . Its easy to spot a director because he wears a headset on his %s and yells things like CAMERA TWO and %s ! A newscast is live television so if you make aor an %s mistake, everyone watching at home on their %s will know!")

    story = story % (adj1, adj2, verbing1, place1, adj3, plnoun1, plnoun2, verb1, adj4, bodypl1, verb2, 
    body1, sword1, adj5, plnoun3)

    print (story)

Я борюсь с этим, я ценю всю помощь. Это доработка

    """ Newscast MadLib by Salem
"""

print ("Here comes a MadLib yall!!!")


noun1 = input("Enter a Noun: " )
noun2 = input("Enter a Noun: " )
noun3 = input("Enter a Noun: " )
noun4 = input("Enter a Noun: " )
noun5 = input("Enter a Noun: " )
adj1 = input("Enter an Adjective: ")
adj2 = input("Enter an Adjective: ")
adj3 = input("Enter an Adjective: ")
adj4 = input("Enter an Adjective: ") 
adj5 = input("Enter an Adjective: ")
verb1 = input("Enter a Verb: ")
verb2 = input("Enter a Verb: ")
verb3 = input("Enter a Verb: ")
verb4 = input("Enter a Verb: ")
verb5 = input("Enter a Verb: ")
adv1 = input("Enter a Adverb: ")
adv2 = input("Enter a Adverb: ")
adv3 = input("Enter a Adverb: ")
adv4 = input("Enter a Adverb: ")
adv5 = input("Enter a Adverb: ")
verbing1 = input("Enter a Verb ending in ing: ")
verbing2 = input("Enter a verb ending in ing: ")
verbing3 = input("Enter a verb ending in ing: ")
verbs1 = input("Enter a verb ending in S: ")
verbs2 = input("Enter a verb ending in S: ")
verbs3 = input("Enter a verb ending in S: ")
pnoun1 = input("Enter a Proper Noun: ")
pnoun2 = input("Enter a Proper Noun: ")
pnoun3 = input("Enter a Proper Noun: ")
pnoun4 = input("Enter a Proper Noun: ")
pnoun5 = input("Enter a Proper Noun: ")
plnoun1 = input("Enter a Plural Noun: ")
plnoun2 = input("Enter a Plural Noun: ") 
plnoun3 = input("Enter a Plural Noun: ")
plnoun4 = input("Enter a Plural Noun: ")
plnoun5 = input("Enter a Plural Noun: ")
body1 = input("Enter a Body Part: ")
body2 = input("Enter a Body Part: ")
body3 = input("Enter a Body Part: ")
body4 = input("Enter a Body Part: ")
body5 = input("Enter a Body Part: ")
bodypl1 = input("Enter a part of the body Plural: ")
bodypl2 = input("Enter a part of the body Plural: ")
bodypl3 = input("Enter a part of the body Plural: ")
emotion1 = input("Enter an Emotion: ")
emotion2 = input("Enter an Emotion: ")
emotion3 = input("Enter an Emotion: ")
historicm1 = input("Enter a Historic Monument: ")
historicm2 = input("Enter a Historic Monument: ")
mansname1 = input("Enter a Mans Name: ")
mansname2 = input("Enter a Mans Name: ")
womname1 = input("Enter a Womans Name: ")
womname2 = input("Enter a Womans Name: ")
occup1 = input("Enter an Occupation: ")
occup2 = input("Enter an Occupation: ")
occup3 = input("Enter an Occupation: ")
shape1 = input("Enter a Shape: ")
shape2 = input("Enter a Shape: ")
shape3 = input("Enter a Shape: ")
rest1 = input("Enter a Restaurant Name: ")
rest2 = input("Enter a Restaurant Name: ")
rest4 = input("Enter a Restaurant Name: ")
liquid1 = input("Enter a type of Liquid: ")
liquid2 = input("Enter a type of Liquid: ")
personir1 = input("Enter the name of someone in the room: ")
personir2 = input("Enter the name of someone in the room: ")
personir4 = input("Enter the name of someone in the room: ")
food1 = input("Enter a type of Food: ")
food2 = input("Enter a type of Food: ")
food3 = input("Enter a type of Food: ")
fperson1 = input("Enter the name of a famous person: ")
fperson2 = input("Enter the name of a famous person: ")
fperson3 = input("Enter the name of a famous person: ")
sword1 = input("Enter a Silly Word: ")
sword2 = input("Enter a Silly Word: ")
sword3 = input("Enter a Silly Word: ")
num1 = input("Enter a Number: ")
num2 = input("Enter a Number: ")
num3 = input("Enter a Number: ")
num4 = input("Enter a Number: ")
num5 = input("Enter a Number: ")
place1 = input("Enter a Place: ")
place2 = input("Enter a Place: ")
place3 = input("Enter a Place: ")
place4 = input("Enter a Place: ")
place5 = input("Enter a Place: ")


def entera(adj1, adj2, verbing1, place1, adj3, plnoun1, plnoun2, verb1, adj4, bodypl1, verb2, body1, sword1, adj5, plnoun3):
    input("Enter a {}: ".format(adj1))
    input("Enter a {}: ".format(adj2))
    input("Enter a {}: ".format(verbing1))
    input("Enter a {}: ".format(place1))
    input("Enter a {}: ".format(adj3))
    input("Enter a {}: ".format(plnoun1))
    input("Enter a {}: ".format(plnoun2))
    input("Enter a {}: ".format(verb1))
    input("Enter a {}: ".format(adj4))
    input("Enter a {}: ".format(bodypl1))
    input("Enter a {}: ".format(verb2))
    input("Enter a {}: ".format(body1))
    input("Enter a {}: ".format(sword1))
    input("Enter a {}: ".format(adj5))
    input("Enter a {}: ".format(plnoun3))
    return (adj1, adj2, verbing1, place1, adj3, plnoun1, plnoun2, verb1, adj4, bodypl1, verb2, body1, sword1, adj5, plnoun3)



story = ("Putting on a newscast might look easy, but it takes a lot of {} work. Go behind the scenes, and you will see dozens of {} workers {} in every direction! Reporters run back and forth between the studio and locations all around {} to cover {} stories and interview {} . They are joined by videographers who operate handheld {} to capture all the action. The anchors are the people who {} behind the news desk and read the stories during the newsccast. They have to look {} on air, so they can often be found getting makeup applied to their {} . The director tells everyone where and when to {} . Its easy to spot a director because he wears a headset on his {} and yells things like CAMERA TWO and {} ! A newscast is live television so if you make aor an {} mistake, everyone watching at home on their {} will know!")

story = story.format(entera("adj1", "adj2", "verbing1", "place1", "adj3", "plnoun1", "plnoun2", "verb1", "adj4", "bodypl1", "verb2", "body1", "sword1", "adj5", "plnoun3"))

print (story)

и это ошибка его бросания

Exception has occurred: IndexError
Replacement index 1 out of range for positional args tuple
  File "C:\Users\hgsal\Documents\python\ML Newscast", line 117, in <module>
    story = story.format(entera("adj1", "adj2", "verbing1", "place1", "adj3", "plnoun1", "plnoun2", "verb1", "adj4", "bodypl1", "verb2", "body1", "sword1", "adj5", "plnoun3"))

3 ответа

Вам нужно будет отложить ввод до строкового формата, а не собирать все переменные заранее

def entera(desc):
    return input("Enter a {}: ".format(desc))

story = "Putting on a newscast might look easy, but it takes a lot of {} work"

story = story.format(entera("Adjective"))

Чтобы запросить только те переменные, которые необходимы истории, нам нужно каким-то образом указать имя каждой переменной, которую использует история, и какой вопрос задать, чтобы заполнить значение этой переменной. Один из способов сделать это - закодировать переменную и вопрос, который нужно задать, являются частью строки истории и использовать ее для управления вопросами пользователя. Вот подход, который делает это:

"""
MadLibs
the madlib story will have fixed text and fill-in-the-blank text that we     
will encoded as placeholders in the form:  {variable_name:question_to_ask}

we use a regular expression of scan the story for all the placeholders and 
- prompt the user for input with the question_to_ask string
- store their response in a dict with the key being the variable_name
  and the value being the user response for that variable

the resulting dictionary will have all the information we need to fill in the blanks

next we modify the original story with another regular expression to remove the
:question_to_ask portion of each placeholder.  

that leaves the story string in the perfect form to call story.format(**values)

the **values notation passes the values dict as a set of keyword arguments 
that .format() uses to fill in the placeholders with the user responses


"""


import re    # bring in regular expression module

story = """
Hi my name is {name1:Name of person in room} and I live in {location:A Location}
where I like to eat {food1:Type Of Food} and watch {tvshow1:A Television Show}
when I am feeling {emotion1:An Emotion}"""

placeholder_pattern = re.compile(r"(?s)\{(.+?):(.+?)\}")
question_pattern = re.compile(r"(?s):[^}]+?}")

values = {}    # start with empty dictionary

placeholders = placeholder_pattern.findall(story)  # find all placeholders
for variable, question in placeholders:            # iterate over each placeholder
    value = input(question + "? ")
    values[variable] = value                       # add variable:value to dict

print("\nThe values dictionary is:", value, "\n")  # debug
story = question_pattern.sub("}", story)           # remove the question from each placeholder
print(story.format(**values))                      # use .format() to fill in placeholders

Запуск программы:


runfile ('C:/demo1.py', wdir='C:')

Имя человека в номере? Фред

Местоположение? библиотека

Тип еды? хот-доги

Телешоу? Безумцы

Эмоция? печальный

Словарь значений: {'name1': 'Fred', 'location': 'the library', 'food1': 'hotdogs', 'tvshow1': 'Mad Men', 'Emotion1': 'sad'} 


Привет, меня зовут Фред, я живу в библиотеке
где я люблю есть хот-доги и смотреть "Безумцев"
когда мне грустно

С помощью ваших комментариев и моего друга Джоша я создал сценарий, который прекрасно работает. Вот окончательный результат. Прошу прощения, есть ненормативная лексика.

""" Newscast MadLib by Salem
"""

print ("Here comes a fucking MadLib yall!!!")


def entera(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o):
    p = input("Enter a fucking {}: ".format(a))
    q = input("Enter a fucking {}: ".format(b))
    r = input("Enter a fucking {}: ".format(c))
    s = input("Enter a fucking {}: ".format(d))
    t = input("Enter a fucking {}: ".format(e))
    u = input("Enter a fucking {}: ".format(f))
    v = input("Enter a fucking {}: ".format(g))
    w = input("Enter a fucking {}: ".format(h))
    x = input("Enter a fucking {}: ".format(i))
    y = input("Enter a fucking {}: ".format(j))
    z = input("Enter a fucking {}: ".format(k))
    ab = input("Enter a fucking {}: ".format(l))
    ac = input("Enter a fucking {}: ".format(m))
    ad = input("Enter a fucking {}: ".format(n))
    ae = input("Enter a fucking {}: ".format(o))
    return (p, q, r, s, t, u, v, w, x, y, z, ab, ac, ad, ae)



story = ("Putting on a newscast might look easy, but it takes a lot of {} work. Go behind the scenes, and you will see dozens of {} workers {} in every direction! Reporters run back and forth between the studio and locations all around {} to cover {} stories and interview {} . They are joined by videographers who operate handheld {} to capture all the action. The anchors are the people who {} behind the news desk and read the stories during the newsccast. They have to look {} on air, so they can often be found getting makeup applied to their {} . The director tells everyone where and when to {} . Its easy to spot a director because he wears a headset on his {} and yells things like CAMERA TWO and {} ! A newscast is live television so if you make a/an {} mistake, everyone watching at home on their {} will know!")

story = story.format(*entera("Adjective", "Adjective", "Verb ending in ING", "Place", "Adjective", "Plural Noun", "Plural Noun", "Verb", "Adjective", "Body Part Plural", "Verb", "Body part", "Silly Word", "Adjective", "Plural Noun"))

print (story)
Другие вопросы по тегам