Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace function
#1
Hey there the replace function is not working.. :( see code below. I have bolded the relevant sections it's towards the bottom of the code.. Getting quite frustrated with this.. Any suggestions would be appreciated. Thanks famalam..  Smile Smile Blush Blush

####START OF CODE


import time
from time import sleep

#First, let's define the introduction. This will ask the name of the player and print the introductory sentence with the name.

name = raw_input("Hey there, what's your name?")
intro = "Welcome to my Udacity, Mad-Libs game " + name

#Defining the blanks that will be replaced, we input four blanks.

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

# The questions that will pop up with the blanks. Fairly striaght forward so far...
questions_easy = '''Python is an __1__ oriented __2__ programming language that has become very popular. It was created by __3__ Van Rossum.Python has been used widely, especially for __4__ learning.'''

questions_medium = '''Python has full support for object-__1__ programming including user-defined __2__, inheritance,
 and run-time binding of methods. Python has an extensive standard __3__, which is one of the main reasons for its popularity.
 Python was first created in __4__ 1989'''

questions_hard = '''Python is intended to be a highly readable __1__. It is designed to have an clear visual __2__,
often using English keywords where other languages use __3__.'''

#Below are the answers.

answers_list_easy = ["object", "dynamic", "Guido", "machine"]
answers_list_medium = ["oriented", "classes", "library", "December"]
answers_list_hard = ["language", "layout", "t-3", "t-4"]

#Now we ask for the level input. The difficulty_level function will then return the selected questions from our questions list.

pick_level = raw_input("Type in your difficulty level, choose from easy, medium and hard. ")

#Defining the selected level and appropriate retun.

def difficulty_level(pick_level):
        if pick_level == "easy":
            print "You chose easy, okay..."
            return questions_easy
        elif pick_level == "medium":
            print "You chose medium, getting there..."
            return questions_medium
        elif pick_level == "hard":
            print "So your a tough nugget, aren't ya!"
            return questions_hard
        else:
            print "What's up doc? *CrunchCrunch* Something the matter?" #That's supposed to be BugsBunny.. :'D
            pass #If the answer is wrong, the code will pass. I'm considering changing this to "return None" as I can then use this later to return the query again.

def return_process(pick_level): #This will return the appropriate answer list depending on which questions are asked.
    if difficulty_level(pick_level) == questions_easy:
        return answers_list_easy
    if difficulty_level(pick_level) == questions_medium:
        return answers_list_medium
    if difficulty_level(pick_level) == questions_hard:
        return answers_list_hard
    pass



def check_answer(user_answer, answers_list, answers_index):
    if user_answer == answers_list[answers_index]:
        return "right_answer"
    return "Wrong"
    pass




def play_game():
    quiz = difficulty_level(pick_level)
    print quiz
    answers_list =  return_process(pick_level)
    blanks_index = 0
    game_over = 0
    answers_index = 0
    guesses = 3

    while blanks_index < len(blanks):
        user_answer = raw_input("So what's your answer to question " + blanks[blanks_index] + "? : ")
        if check_answer(user_answer,answers_list,answers_index) == "right_answer":
            print "nice job! that is the right answer!\n"
            replaced_quiz = quiz.replace(blanks[blanks_index], user_answer.upper()) #This should replace the blanks with the user-answer, why is is not working??? :(
            blanks_index += 1
            answers_index += 1
            guesses = 3
            print replaced_quiz
            if blanks_index == len(blanks):
                print "We got ourselves a winner!!"
                time.sleep (3)
        else:
            guesses -= 1
            if guesses == game_over:
                print "Wawaooohh, you lost..... :("
                time.sleep (3)
                break
            else:
                print "please try again. one guess down..."

play_game()
Reply
#2
Please, post your code in CODE tags.
Please, post only short runnable code that reproduce the problem - e.g. no need for multiple difficulty levels, no user input (e.g. hard-code it), etc.
Include the full traceback you get (if any) in ERROR tags. Not working is not clear - you get error or the result is not as expected, etc.
Reply
#3
Thanks for the heads up Buran. See the sectioned code in tags below. 
Thing is there's no error. It just doesn't replace... 
So it gives the quiz without making any changes.

def play_game():
    quiz = difficulty_level(pick_level)
    print quiz
    answers_list =  return_process(pick_level)
    blanks_index = 0
    game_over = 0
    answers_index = 0
    guesses = 3

    while blanks_index < len(blanks):
        user_answer = raw_input("So what's your answer to question " + blanks[blanks_index] + "? : ")
        if check_answer(user_answer,answers_list,answers_index) == "right_answer":
            print "nice job! that is the right answer!\n"
            replaced_quiz = quiz.replace(blanks[blanks_index], user_answer.upper()) #This should replace the blanks with the user-answer, why is is not working??? :(
            blanks_index += 1
            answers_index += 1
            guesses = 3
            print replaced_quiz
            if blanks_index == len(blanks):
                print "We got ourselves a winner!!"
                time.sleep (3)
        else:
            guesses -= 1
            if guesses == game_over:
                print "Wawaooohh, you lost..... :("
                time.sleep (3)
                break
            else:
                print "please try again. one guess down..."
Moderator Larz60+: Use python tag, not icode -- I fixed it this time
Reply
#4
Well, that's definitely not runnable example, but anyway.
The problem is that you have different number of undescores in the blanks and in the questions. They are different even between different elements in blanks:
e.g. ___1___ and __1__ (first is whit three, and in the question is with two)
__4__ (only two underscores)

and as a side note, in your case minimal runnable code snippet would be

blanks = ["___1___","___2___","___3___", "__4__"]
blanks_index = 0
quiz = '''Python is an __1__ oriented __2__ programming language that has become very popular. It was created by __3__ Van Rossum.Python has been used widely, especially for __4__ learning.'''
replaced_quiz = quiz.replace(blanks[blanks_index], 'object'.upper()) #This should replace the blanks with the user-answer, why is is not working??? :(
print replaced_quiz
Output:
Python is an __1__ oriented __2__ programming language that has become very popu lar. It was created by __3__ Van Rossum.Python has been used widely, especially for __4__ learning.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  basic random number generator in replace function krug123 2 2,045 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Beginner problem, replace function with for loop Motley_Cow 9 4,632 Sep-13-2019, 06:24 AM
Last Post: Motley_Cow
  Help trying to replace values in a function Rochense 1 2,309 Apr-01-2019, 09:59 PM
Last Post: nilamo
  Search & Replace - Newlines Added After Replace dj99 3 3,403 Jul-22-2018, 01:42 PM
Last Post: buran
  replace-function for selveral variables.. Specdrum 2 2,634 Jul-04-2018, 10:41 AM
Last Post: Specdrum
  Is there a built-in function to replace multiple bytes? Raptor88 4 34,190 Feb-25-2017, 03:48 AM
Last Post: Raptor88

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020