0

I'm working on a quiz program and need some help. I'm trying to replace words one at a time, but Python isn't saving the previously replaced string. Here is a mini example of what I mean:

replacedQuiz=""
easyQuiz = """
You can change a string variable to an integer by typing (__1__)
in front of the variable. It also works vice versa, you can change an 
integer
variable to a string by typing (__2__). This is important to remember before
you __3__ strings together, or else a TypeError will occur. While adding an
integer to a string, it is important to separate it using a __4__ (use the
symbol). \n"""

def replaceWord(replaced, quiz, numCount):
    if numCount == 1:
        replaced = quiz.replace("__1__", "int")
    if numCount == 2:
        replaced = replaced.replace("__2__", "str")
    if numCount == 3:
        replaced= replaced.replace("__3__", "concatenate")
    if numCount == 4:
        replaced= replaced.replace("__4__", ",")    
    print replaced

def easy():
    QCount=1
    print easyQuiz

    while QCount < 5:
        replaceWord(replacedQuiz, easyQuiz, QCount)
        QCount += 1

print easy()

I thought that by making a String called replacedQuiz, it would save the first replacement and then I could continue replacing the words inside the quiz and updating it. Please help! I don't know where I'm going wrong

2
  • In your function, shouldn't replacedQuiz be replaced? Commented Apr 30, 2018 at 19:34
  • Yea sorry, I updated it to be a smaller version of what I had and missed that. I have tried that though and it still didn't work. (I edited it ^^)
    – Grace D
    Commented Apr 30, 2018 at 19:40

1 Answer 1

1

You seem to have made a slight mistake in the scope of your variable replacedQuiz (it'd certainly suggest that you check out some explanation of this topic). Basically, you are replacing replacedQuiz by its new value only within your current function. Your other functions only have access to the global value you defined earlier. There are several ways to fix this (e.g. the global keyword) but the standard way would be to return the new replacedQuiz from your function.

To do so, add the following line to the end of your replaceWord function:

return replacedQuiz

This tells Python to use this value at the line it was called at. You can then define a new value for replacedQuiz within easy by just defining it as the returned value:

replacedQuiz = replaceWord(replacedQuiz, easyQuiz, QCount)
2
  • That worked!!! Thank you so much, I've been stuck on this for a few hours!
    – Grace D
    Commented Apr 30, 2018 at 21:42
  • No problem, just glad it worked out. Hopefully it has taught you something ;), we all had to start somewhere. If you accept the answer, others won't see it come by as unsolved any more and they can help someone else :).
    – Martijn
    Commented Apr 30, 2018 at 21:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.