Python Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|Last
Lessons:-INTRODUCTION

1.2. THE PYTHON INTERPRETER AND IDLE, PART I 9
storyFormat string will be used later to provide a format into which substitutions are made.
The parts of the string enclosed in braces are places a substitute string will be inserted later. The
substituted string will come from a custom dictionary that will contain the user’s definitions of
these words. The words in the braces: {animal}, {food}, {city}, indicate that "animal", "food",
and "city" are words in a dictionary. This custom dictionary will be created in the program and
contain the user’s definitions of these words. These user’s definitions will be substituted later in
the format string where each {...} is currently.
def tellStory(): 22
userPicks = dict() 23
addPick(’animal’, userPicks) 24
addPick(’food’, userPicks) 25
addPick(’city’, userPicks) 26
story = storyFormat.format(**userPicks) 27
print(story) 28
22 def is short for def inition. This line is the heading of a def inition, which makes the name
tellStory becomes def ined as a short way to refer to the sequence of statements that start
indented on line 23, and continue through line 27.
23 The equal sign tells the computer that this is another assignment statement. The computer will
now associate the name userPicks with a new empty dictionary created by the Python code
dict().
24-26 addPick is the name for a sequence of instructions defined on lines 29-31 for adding another
definition to a dictionary, based on the user’s input. The result of these three lines is to add
definitions for each of the three words ’animal’, ’food’, and ’city’ to the dictionary called userPicks.
27 Assign the name story to a string formed by substituting into storyFormat using definitions
from the dictionary userPicks, to give the user’s customized story.
28 This is where all the work becomes visible: Print the story string to the screen.
def addPick(cue, dictionary): 30
’’’Prompt for a user response using the cue string, 31
and place the cue-response pair in the dictionary. 32
’’’ 33
prompt = ’Enter an example for ’ + cue + ’: ’ 34
response = input(prompt) 35
dictionary[cue] = response 36
30 This line is the heading of a definition, which gives the name addPick as a short way to refer to
the sequence of statements indented on line 34-36. The name addPick is followed by two words
in parenthesis, cue and dictionary. These two words are associated with an actual cue word
and dictionary given when this definition is invoked in lines 24-26.
31-33 A documentation comment for the addPick definition.
34 The plus sign here is used to concatenate parts of the string assigned to the name prompt. The
current value of cue is placed into the string.
35 The right-hand-side of this equal sign causes an interaction with the user. The prompt string
is printed to the computer screen, and the computer waits for the user to enter a line of text.
That line of text then becomes a string inside the program. This string is assigned to the name
response.
36 The left-hand-side of the equal sign is a reference to the definition of the cue word in the dictionary.
The whole line ends up making the definition of the current cue word become the response typed
by the user.
tellStory() 38
input("Press Enter to end the program.") 39
38 The definition of tellStory above does not make the computer do anything besides remember
what the instruction tellStory means. It is only in this line, with the name, tellStory, followed
by parentheses, that the whole sequence of remembered instructions are actually carried out.

 
 
 
image
Anil  Bist

Skills    Python

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)