简体   繁体   中英

replacing a string in python list with another string

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)

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

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)

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM