Mar-29-2017, 07:52 AM
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..
####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()




####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()