0

currently my loops stops at replacing 1 with the answer. how do i loop it so it replaces all 4 blanks with the 4 answers?

easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China"

blanks = ["___1___","___2___","___3___","___4___"]

easyAnswer = ["195","Russia","Antartica","Beijing"]

#asks users to choose difficulty level
question = raw_input("Shall we start? Easy, Medium, Hard?")


if question == "Easy" or question == "easy":
    answerChoice = easyAnswer

answerChoice = answerList(question)
newprompt = []
newlist = []
index = 0
maxblanks = 3

for quizwords in difficulty(question).split():

    if quizwords == "1" :
        quizwords = quizwords.replace("1",answerChoice[index])


    elif quizwords == "2" :
        quizwords = quizwords.replace("2",answerChoice[index])

        index = index + 1
    newlist.append(quizwords)
    joinedlist = " ".join(newlist)
4
  • 3
    What's answerList and difficulty? Commented Feb 1, 2018 at 15:37
  • You probably want to use string formatting here: pyformat.info Commented Feb 1, 2018 at 15:38
  • Also, if quizwords == "1", you can just do quizwords = answerChoice[index] instead of replace, but is this really what you intended? Commented Feb 1, 2018 at 15:38
  • 1
    If you expect any help, post a MCVE including expected results (stackoverflow.com/help/mcve) Commented Feb 1, 2018 at 15:38

4 Answers 4

2

You can use string formatting

def easyQuiz(vals):
      return """
There are {} countries in the world.
The countries with the biggest landmass is {}.
The coldest continent in the world is {}.
{} is the capital of China""".format(*vals)

print(easyQuiz(("___1___","___2___","___3___","___4___")))
# There are ___1___ countries in the world.
# The countries with the biggest landmass is ___2___.
# The coldest continent in the world is ___3___.
# ___4___ is the capital of China

print(easyQuiz(("195","Russia","Antartica","Beijing")))
# There are 195 countries in the world.
# The countries with the biggest landmass is Russia.
# The coldest continent in the world is Antartica.
# Beijing is the capital of China
Sign up to request clarification or add additional context in comments.

6 Comments

thank you! what does the * in the .format(*vals) mean?
@nicholasgwee you are welcome. It's tuple unpacking.
may i ask how can i replace only the 2nd {} in the question although there are four values
@nicholasgwee as a simple approach you could still replace all 4, but for 1, 2, 4 just insert the placeholders. E.g. print(easyQuiz(("___1___","Russia","___3___","___4___"))).
Thank you so much i managed to format the blanks based on users input for the correct answers! much appreciated!
|
0

You can zip the blanks with the corresponding easyAnswer and replace those in a loop.

for blank, answer in zip(blanks, easyAnswer):
    easyQuiz = easyQuiz.replace(blank, answer)

Or zip to a dict and use re.sub with a callback:

answer_dict = dict(zip(blanks, easyAnswer))
result = re.sub("___\d___", lambda m: answer_dict[m.group()], easyQuiz)

Comments

0

You can use string formatting with regex:

import re
easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China"
easyAnswer = ["195","Russia","Antartica","Beijing"]
answers = re.sub('___\d+___', '{}', easyQuiz).format(*easyAnswer)

Output:

'There are 195 countries in the world. The countries with the biggest landmass is Russia. The coldest continent in the world is Antartica. Beijing is the capital of China'

Comments

0

Question is a bit confusing, especially 'difficulty' and 'answerList'. Assuming, 'answerList' returns a list of four answers. Your loop will depend upon difficulty(question).split(). My hunch is your loop is iterating only once, and the index is not getting updated after that hence loops stops at replacing 1 with the answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.