1. Programs are composed of modules.
2. Modules contain statements.
3. Statements contain expressions.
4. Expressions create and process objects.
Handling Errors by Testing Inputs
"""
(1) 判断输入
"""
while True: reply = input(‘Enter text:‘)
if reply == ‘stop‘: break elif not reply.isdigit(): print(‘Bad!‘ * 8) else: print(int(reply) ** 2) print(‘Bye‘) """
(2) 输入不是数字怎么办?
""" # 方法一
while True: reply = input(‘Enter text:‘) if reply == ‘stop‘: break
# 如果非法str,执行except try: num = int(reply) except: print(‘Bad!‘ * 8) else: print(num ** 2) print(‘Bye‘) # 方法二 while True: reply = input(‘Enter text:‘) if reply == ‘stop‘: break elif not reply.isdigit(): # 如果不是数字 print(‘Bad!‘ * 8) else: # 如果是数字 num = int(reply) if num < 20: print(‘low‘) else: print(num ** 2) print(‘Bye‘)