patternpythonMinor
Fill in the blanks game
Viewed 0 times
gamethefillblanks
Problem
So I've coded a fill in the blanks game but my last procedure,
Trying to figure out how to divide it into two functions:
```
import time
from time import sleep
name = raw_input("Hey there, what's your name?") #Basic raw_input
intro = "Welcome to my Udacity, Mad-Libs game " + name #text plus value of "name"
blanks = ["__1__","__2__","__3__", "__4__"]
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__.
Essencially the first step when creating a new program is creating a "draft" code also called __3__. This will allow you to have a basic idea on how the code
should be structured. When your code is complete and ready to testing for __4__ inputs and outputs.'''
answers_list_easy = ["object", "dynamic", "Guido", "machine"]
answers_list_medium = ["oriented", "classes", "library", "December"]
answers_list_hard = ["language", "layout", "pseudocode", "valid"]
game_data = {
'easy': {
'quiz': questions_easy,
'answers': answers_list_easy,
'message': "you chose easy, okay"
},
'medium': {
'quiz': questions_medium,
'answers': answers_list_medium,
'message': "You chose medium, a fair challenge"
},
'hard': {
'quiz': questions_hard,
'answers': answers_list_hard,
'message': "So you're a tough nugget aren't ya!!"
}
}
def get_level(): #Inputs easy, medium or hard. Output, Relevant
play_game() is far too long.Trying to figure out how to divide it into two functions:
- Initialize
- process_answer
```
import time
from time import sleep
name = raw_input("Hey there, what's your name?") #Basic raw_input
intro = "Welcome to my Udacity, Mad-Libs game " + name #text plus value of "name"
blanks = ["__1__","__2__","__3__", "__4__"]
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__.
Essencially the first step when creating a new program is creating a "draft" code also called __3__. This will allow you to have a basic idea on how the code
should be structured. When your code is complete and ready to testing for __4__ inputs and outputs.'''
answers_list_easy = ["object", "dynamic", "Guido", "machine"]
answers_list_medium = ["oriented", "classes", "library", "December"]
answers_list_hard = ["language", "layout", "pseudocode", "valid"]
game_data = {
'easy': {
'quiz': questions_easy,
'answers': answers_list_easy,
'message': "you chose easy, okay"
},
'medium': {
'quiz': questions_medium,
'answers': answers_list_medium,
'message': "You chose medium, a fair challenge"
},
'hard': {
'quiz': questions_hard,
'answers': answers_list_hard,
'message': "So you're a tough nugget aren't ya!!"
}
}
def get_level(): #Inputs easy, medium or hard. Output, Relevant
Solution
from time import sleepSince you already imported
time and are only calling time.sleep, this import is obsolete.name = raw_input("Hey there, what's your name?") #Basic raw_input
intro = "Welcome to my Udacity, Mad-Libs game " + name #text plus value of "name"The comments at the end of the lines do not tell you anything that is not already in the code, so you don't need them. Comments should usually tell you, why something is done, not what is done. What is done is already in the code.
answers_list_easy = ["object", "dynamic", "Guido", "machine"]Having the type in the variable name is not needed, and rather bad practice. You might want to change the type later, for example to a tuple. Using a plural name (like
answers_easy or easy_answers) already tells you (and possibly other developers) that the variable contains a collection, probably a sequence of answers (and a list is a kind of sequence).game_data = {
'easy': {
'quiz': questions_easy,
'answers': answers_list_easy,
'message': "you chose easy, okay"
},
'medium': {
'quiz': questions_medium,
'answers': answers_list_medium,
'message': "You chose medium, a fair challenge"
},
'hard': {
'quiz': questions_hard,
'answers': answers_list_hard,
'message': "So you're a tough nugget aren't ya!!"
}
}This is not pretty, and probably useless. Since you need to access these values via strings as keys anyway, you might as well just access the global variables. These nested dictionaries do not really simplify the access to the variables, rather make it harder, and since at no point in the future
"easy" will suddenly point towards questions_medium, you do not need to abstract the variable access.def get_level(): #Inputs easy, medium or hard. Output, Relevant questions and answers.
pick_level = raw_input("Type in your difficulty level. Choose from easy, medium or hard")
while pick_level not in ["easy", "medium", "hard"]:
pick_level = raw_input("Invalid input, please try again. Choose from easy, medium or hard")
print game_data[pick_level]['message']
return pick_levelPrinting the level here is a side effect that you might want to prevent. The method should do only what its name implies, which is getting the level. A better name might be
ask_level or input_level, since get is normally used for just returning a value that is already there. Then you can print the return value where you call this method.def check_answer(user_answer, answers_list, answers_index):
if user_answer == answers_list[answers_index]:
return "right_answer"
return "Wrong"
passThis name is not good, because it does not tell you what kind of answer is being checked, and what a positive result means. A better name would be
is_answer_correct, or just is_correct if the parameter names are explicit enough (which I would say here they are).Considering that the method just does a simple logical comparison with values that are all provided as parameters, you might just remove the method and replace its calls with the comparison.
As already hinted by kyrill in a comment, the method should not return strings (which are even very inconsistent here), but
Trueor False boolean values. Then you can handle printing out the strings where you call the method depending on its result. And the pass does not do anything, actually it never does anything, but everything that comes after a return will not be reached.you_lost and you_win are bad method names, and the methods alltogether are obsolete, since it is more readible to just see these 2 lines in your code than not knowing what you_lost() actually does.blanks_index = 0
# [...]
while blanks_index < len(blanks):
# your codeHere you should iterate using a for-loop:
for blank in blanks:
# your codeCode Snippets
from time import sleepname = raw_input("Hey there, what's your name?") #Basic raw_input
intro = "Welcome to my Udacity, Mad-Libs game " + name #text plus value of "name"answers_list_easy = ["object", "dynamic", "Guido", "machine"]game_data = {
'easy': {
'quiz': questions_easy,
'answers': answers_list_easy,
'message': "you chose easy, okay"
},
'medium': {
'quiz': questions_medium,
'answers': answers_list_medium,
'message': "You chose medium, a fair challenge"
},
'hard': {
'quiz': questions_hard,
'answers': answers_list_hard,
'message': "So you're a tough nugget aren't ya!!"
}
}def get_level(): #Inputs easy, medium or hard. Output, Relevant questions and answers.
pick_level = raw_input("Type in your difficulty level. Choose from easy, medium or hard")
while pick_level not in ["easy", "medium", "hard"]:
pick_level = raw_input("Invalid input, please try again. Choose from easy, medium or hard")
print game_data[pick_level]['message']
return pick_levelContext
StackExchange Code Review Q#159331, answer score: 3
Revisions (0)
No revisions yet.