📄 node10.html
字号:
<TD align=left>sorts list</TD></TR>
<TR>
<TD align=left><TT>list.index("value")</TT></TD>
<TD align=left>returns the index of the first place that <TT>"value"</TT>
occurs</TD></TR>
<TR>
<TD align=left><TT>list.append("value")</TT></TD>
<TD align=left>adds an element <TT>"value"</TT> at the end of the
list</TD></TR></TBODY></TABLE>
<P>This next example uses these features in a more useful way: <PRE>menu_item = 0
list = []
while menu_item != 9:
print "--------------------"
print "1. Print the list"
print "2. Add a name to the list"
print "3. Remove a name from the list"
print "4. Change an item in the list"
print "9. Quit"
menu_item = input("Pick an item from the menu: ")
if menu_item == 1:
current = 0
if len(list) > 0:
while current < len(list):
print current,". ",list[current]
current = current + 1
else:
print "List is empty"
elif menu_item == 2:
name = raw_input("Type in a name to add: ")
list.append(name)
elif menu_item == 3:
del_name = raw_input("What name would you like to remove: ")
if del_name in list:
item_number = list.index(del_name)
del list[item_number]
#The code above only removes the first occurance of
# the name. The code below from Gerald removes all.
#while del_name in list:
# item_number = list.index(del_name)
# del list[item_number]
else:
print del_name," was not found"
elif menu_item == 4:
old_name = raw_input("What name would you like to change: ")
if old_name in list:
item_number = list.index(old_name)
new_name = raw_input("What is the new name: ")
list[item_number] = new_name
else:
print old_name," was not found"
print "Goodbye"
</PRE>
<P>And here is part of the output:
<P><PRE>--------------------
1. Print the list
2. Add a name to the list
3. Remove a name from the list
4. Change an item in the list
9. Quit
Pick an item from the menu: 2
Type in a name to add: Jack
Pick an item from the menu: 2
Type in a name to add: Jill
Pick an item from the menu: 1
0 . Jack
1 . Jill
Pick an item from the menu: 3
What name would you like to remove: Jack
Pick an item from the menu: 4
What name would you like to change: Jill
What is the new name: Jill Peters
Pick an item from the menu: 1
0 . Jill Peters
Pick an item from the menu: 9
Goodbye
</PRE>
<P>That was a long program. Let's take a look at the source code. The line
<TT>list = []</TT> makes the variable <TT>list</TT> a list with no items (or
elements). The next important line is <CODE>while menu_item != 9:</CODE>. This
line starts a loop that allows the menu system for this program. The next few
lines display a menu and decide which part of the program to run.
<P>The section: <PRE>current = 0
if len(list) > 0:
while current < len(list):
print current,". ",list[current]
current = current + 1
else:
print "List is empty"
</PRE>goes through the list and prints each name. <CODE>len(list_name)</CODE>
tell how many items are in a list. If <TT>len</TT> returns <CODE>0</CODE> then
the list is empty.
<P>Then a few lines later the statement <TT>list.append(name)</TT> appears. It
uses the <TT>append</TT> function to add a item to the end of the list. Jump
down another two lines and notice this section of code: <PRE>item_number = list.index(del_name)
del list[item_number]
</PRE>Here the <TT>index</TT> function is used to find the index value that will
be used later to remove the item. <CODE>del list[item_number]</CODE> is used to
remove a element of the list.
<P>The next section <PRE>old_name = raw_input("What name would you like to change: ")
if old_name in list:
item_number = list.index(old_name)
new_name = raw_input("What is the new name: ")
list[item_number] = new_name
else:
print old_name," was not found"
</PRE>uses <TT>index</TT> to find the <CODE>item_number</CODE> and then puts
<CODE>new_name</CODE> where the <CODE>old_name</CODE> was.
<P>
<H1><A name=SECTION001030000000000000000>Examples</A> </H1>
<P>test.py <PRE>## This program runs a test of knowledge
true = 1
false = 0
# First get the test questions
# Later this will be modified to use file io.
def get_questions():
# notice how the data is stored as a list of lists
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and everything?","42"],\
["What is a three letter word for mouse trap?","cat"]]
</PRE><PRE># This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question(question_and_answer):
#extract the question and the answer from the list
question = question_and_answer[0]
answer = question_and_answer[1]
# give the question to the user
given_answer = raw_input(question)
# compare the user's answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was:",answer
return false
</PRE><PRE># This will run through all the questions
def run_test(questions):
if len(questions) == 0:
print "No questions were given."
# the return exits the function
return
index = 0
right = 0
while index < len(questions):
#Check the question
if check_question(questions[index]):
right = right + 1
#go to the next question
index = index + 1
#notice the order of the computation, first multiply, then divide
print "You got ",right*100/len(questions),"% right out of",len(questions)
#now lets run the questions
run_test(get_questions())
</PRE>
<P>Sample Output: <PRE>What color is the daytime sky on a clear day?green
Incorrect, correct was: blue
What is the answer to life, the universe and everything?42
Correct
What is a three letter word for mouse trap?cat
Correct
You got 66 % right out of 3
</PRE>
<P>
<H1><A name=SECTION001040000000000000000>Exercises</A> </H1>
<P>Expand the test.py program so it has menu giving the option of taking the
test, viewing the list of questions and answers, and an option to Quit. Also,
add a new question to ask, "What noise does a truly advanced machine make?" with
the answer of "ping".
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html"
name=tex2html276><IMG align=bottom alt=next border=0 height=24
src="node10_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html272><IMG align=bottom alt=up border=0 height=24
src="node10_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html266><IMG align=bottom alt=previous border=0 height=24
src="node10_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html274><IMG align=bottom alt=contents border=0 height=24
src="node10_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node11.html"
name=tex2html277>For Loops</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html273>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node9.html"
name=tex2html267>Defining Functions</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html275>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -