PythonÈëÃŵÄ36¸öÀý×Ó Ö® 35
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 039
while True:
try:
x = int(raw_input('Input a number:'))
y = int(raw_input('Input a number:'))
z = x / y
except ValueError, ev:
print 'That is not a valid number.', ev
except ZeroDivisionError, ez:
print 'Divisor is zero:', ez
except:
print 'Unexpected error.'
raise
else:
print 'There is no error.'
break
# end of try
# end of while
print x,'/',y,'=',x/y
output£º
Input a number:w
That is not a valid number. invalid literal for int() with base 10: 'w'
Input a number:9
Input a number:0
Divisor is zero: integer division or modulo by zero
Input a number:9
Input a number:3
There is no error.
9 / 3 = 3
Ïà¹ØÎĵµ£º
# 015
# ĬÈϲÎÊýµÄ±¾ÖÊÊÇ:
# ²»ÂÛÄãÊÇ·ñÌṩ¸øÎÒÕâ¸ö²ÎÊý,ÎÒ¶¼ÊÇÒªÓÃËü,
# ÄÇôÎÒ¾ÍÒªÏëºÃÔÚÄã²»ÏòÎÒÌṩ²ÎÊýµÄʱºòÎÒ¸ÃʹÓÃʲô¡£
# »òÕߣ¬ÕâÑùÀ´Àí½â£º
# ÓÐһЩ²ÎÊýÔÚÒ»°ãÇé¿öÏÂÊÇÔ¼¶¨Ë׳ɵģ¬
# µ«£¬ÔÚ¼«ÉÙÇé¿öÏ»áÓÐһЩºÜÓиöÐÔµÄÈË»á´òÆÆ´«Í³¶ø°´ÕÕ×Ô¼ºµÄÏ°¹ßÀ´×öÊÂ
def theFirstDayInAWeek(theDay = 'Sunda ......
Ä£¿éÕⶫÎ÷ºÃÏñûʲôºÃ½²µÄ£¬ÎÞ·ÇÊDZ£´æÒ»·ÝÎļþ£¬È»ºóÔÚÁíÒ»·ÝÎļþÖÐÓÃimport ºÍfrom ** import **(*)¾ÍÐÐÁË¡£
ÕâÒ»ÕÂÖ÷Òª½²µ½ÁËϸ½Ú£¬µ¼ÈëÄ£¿éPythonÀïÃæÊÇʲô´¦ÀíµÄ£¬import ºÍ from ** import **ÓÐʲô²»Ò»Ñù¡£»¹ÓоÍÊÇÔö¼ÓÁËreload()Õâ¸öº¯ÊýµÄʹÓÃ˵Ã÷¡£
ÒÔÇ°¿´µ½ÄÄÀï˵¾¡Á¿Ê¹ÓÃimport¶ø²»ÒªÊ¹ÓÃfrom ** import * ......
¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 024
dict1 = {
'5064001':'Mememe',
'5064002':'tutu',
'5064003':'thrthr',
'5064004':'fofo'
}
print dict1['5064003']
# Ò²¿ÉÒÔʹÓÃÕûÐÍ×÷ΪΨһµÄ±àºÅ
dict2 = {
5064001:'Mememe',
506400 ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
raiseÓÐÁ½¸ö²ÎÊý£¬µÚÒ»¸öÊÇÓÉÎÒÃÇ×Ô¼º¶¨ÒåµÄÒì³£ÀàÐÍ£¬µÚ¶þ¸öÊǹØÓÚ´ËÒì³£µÄÉÙÁ¿ËµÃ÷ÐÅÏ¢¡£
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# ......