louielouielouie
louielouielouie

Reputation: 39

After something is added to a string, how do I print the modification as well as the rest of the string?

So the title might not make sense. But here is the code:

def play_game(ml_string, blanks, selectedLevel):


replaced = []
ml_string = ml_string.split()
currentQuestion = 0


for blank in ml_string:
    replacement = blank_in_quiz(blank, blanks,)
    if replacement != None:
        user_input = raw_input("Type in the answer for blank " + replacement + " ")
        while user_input != allanswers[selectedLevel][currentQuestion]:
            print "Incorrect!"
            user_input = raw_input("Type in the answer for blank " + replacement + " ")
        else:
            blank = blank.replace(replacement, user_input)
            replaced.append(blank)
            print "\nCorrect!\n"
            print " ".join(replaced + [currentQuestion,ml_string])
            currentQuestion = currentQuestion + 1
    else:
        replaced.append(blank)
replaced = " ".join(replaced)
print replaced

Essentially what this does is take this string, which is ml_string:

"The movie __1__ is a war movie directed by __2__ Nolan about the __3__ and French armies stranded on the __4__ of Dunkirk while the __5__ army closed in on them."

And once the user adds the correct answer to the blank, I am trying to print out the answer filled in the blank, as well as the rest of the quiz with the blanks they haven't answered yet.

I am a beginner at python, but I've always struggled with lists and index values. If you would like to view the entirety: https://repl.it/KTJh/16

Line 55 is what I'm having trouble with. Thanks for any kind of advice.

Upvotes: 1

Views: 61

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177755

Assuming you are using the latest Python to learn (3.6), your could use f-strings. The items in curly braces can be most Python expressions. In this case, they index a word list:

import textwrap

def paragraph(words):
    s = f'The movie {words[0]} is a war movie directed by {words[1]} Nolan about the {words[2]} and French armies stranded on the {words[3]} of Dunkirk while the {words[4]} army closed in on them.'
    print()
    print(textwrap.fill(s))

words = '__1__ __2__ __3__ __4__ __5__'.split()
paragraph(words)
words[0] = 'Dunkirk'
paragraph(words)

Output:

The movie __1__ is a war movie directed by __2__ Nolan about the __3__
and French armies stranded on the __4__ of Dunkirk while the __5__
army closed in on them.

The movie Dunkirk is a war movie directed by __2__ Nolan about the
__3__ and French armies stranded on the __4__ of Dunkirk while the
__5__ army closed in on them.

Upvotes: 0

wwii
wwii

Reputation: 23753

You could use string formatting to create your string with placeholders (replacement_field) that get filled in with some predefined variables, as the users answers you just change the variables. The format specification allows named placeholders

s = "The movie {ans1} is a war movie directed by {ans2} Nolan about the {ans3} and French armies stranded on the {ans4} of Dunkirk while the {ans5} army closed in on them."

Which makes it convenient to fill in the placeholders with a dictionary

d = {'ans1' : '__1__', 'ans2' : '__2__',
     'ans3' : '__3__', 'ans4' : '__4__',
     'ans5' : '__5__'}

You use it like this:

>>> s.format(**d)
'The movie __1__ is a war movie directed by __2__ Nolan about the __3__ and French armies stranded on the __4__ of Dunkirk while the __5__ army closed in on them.'

Change the answers like this

>>> d['ans1'] = 'Ziegfield Follies'
>>> s.format(**d)
'The movie Ziegfield Follies is a war movie directed by __2__ Nolan about the __3__ and French armies stranded on the __4__ of Dunkirk while the __5__ army closed in on them.'
>>>

Upvotes: 2

Related Questions