⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node18.html

📁 Pythone Library reference. it is ok and simple.
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0062)http://www.honors.montana.edu/~jjc/easytut/easytut/node18.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by:  Nikos Drakos, CBLU, University of Leeds* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from:  Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Dealing with the imperfect (or how to handle errors)</TITLE>
<META content="Dealing with the imperfect (or how to handle errors)" 
name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK 
href="node18_files/easytut.css" rel=STYLESHEET><LINK href="node19.html" 
rel=next><LINK href="node17.html" rel=previous><LINK href="easytut.html" 
rel=up><LINK href="node19.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node19.html" 
name=tex2html381><IMG align=bottom alt=next border=0 height=24 
src="node18_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html377><IMG align=bottom alt=up border=0 height=24 
src="node18_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html" 
name=tex2html371><IMG align=bottom alt=previous border=0 height=24 
src="node18_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html379><IMG align=bottom alt=contents border=0 height=24 
src="node18_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node19.html" 
name=tex2html382>The End</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html378>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html" 
name=tex2html372>File IO</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html380>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A 
name=CHILD_LINKS><STRONG>Subsections</STRONG></A> 
<UL>
  <LI><A 
  href="http://www.honors.montana.edu/~jjc/easytut/easytut/node18.html#SECTION001810000000000000000" 
  name=tex2html383>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>

<H1><A name=SECTION001800000000000000000>Dealing with the imperfect (or how to 
handle errors)</A> </H1>
<P>So you now have the perfect program, it runs flawlessly, except for one 
detail, it will crash on invalid user input. Have no fear, for Python has a 
special control structure for you. It's called <CODE>try</CODE> and it tries to 
do something. Here is an example of a program with a problem: <PRE>print "Type Control C or -1 to exit"
number = 1
while number != -1:
    number = int(raw_input("Enter a number: "))
    print "You entered: ",number
</PRE>
<P>Notice how when you enter <CODE>@#&amp;</CODE> it outputs something like: <PRE>Traceback (innermost last):
  File "try_less.py", line 4, in ?
    number = int(raw_input("Enter a number: "))
ValueError: invalid literal for int(): @#&amp;
</PRE>
<P>As you can see the <CODE>int</CODE> function is unhappy with the number 
<CODE>@#&amp;</CODE> (as well it should be). The last line shows what the 
problem is; Python found a <CODE>ValueError</CODE>. How can our program deal 
with this? What we do is first: put the place where the errors occurs in a 
<CODE>try</CODE> block, and second: tell Python how we want 
<CODE>ValueError</CODE>s handled. The following program does this: <PRE>print "Type Control C or -1 to exit"
number = 1
while number != -1:
    try:
        number = int(raw_input("Enter a number: "))
        print "You entered: ",number
    except ValueError:
        print "That was not a number."
</PRE>
<P>Now when we run the new program and give it <CODE>@#&amp;</CODE> it tells us 
``That was not a number.'' and continues with what it was doing before. 
<P>When your program keeps having some error that you know how to handle, put 
code in a <CODE>try</CODE> block, and put the way to handle the error in the 
<CODE>except</CODE> block. 
<P>
<H1><A name=SECTION001810000000000000000>Exercises</A> </H1>
<P>Update at least the phone numbers program so it doesn't crash if a user 
doesn't enter any data at the menu. 
<P>
<HR>
<!--Navigation Panel--><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node19.html" 
name=tex2html381><IMG align=bottom alt=next border=0 height=24 
src="node18_files/next.png" width=37></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html377><IMG align=bottom alt=up border=0 height=24 
src="node18_files/up.png" width=26></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html" 
name=tex2html371><IMG align=bottom alt=previous border=0 height=24 
src="node18_files/prev.png" width=63></A> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html379><IMG align=bottom alt=contents border=0 height=24 
src="node18_files/contents.png" width=65></A> <BR><B>Next:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node19.html" 
name=tex2html382>The End</A> <B>Up:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html" 
name=tex2html378>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node17.html" 
name=tex2html372>File IO</A> &nbsp; <B><A 
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html" 
name=tex2html380>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 + -