#How to set an if function:

if Condition:

suite  (if the condition is True)

statement (no matter the condition is True or False)

螢幕快照 2016-06-27 下午1.27.48

#Introduction of elif and else:

if condition1:

suite1 (if condition1 is True)

elif condition2: (if condition1 is False)

suite2 (if condition2 is True)

else: (if condition1 & 2 are both False)

suite3 (if condition1 & 2 are both False)

statement (no matter condition is True or False)

螢幕快照 2016-06-27 下午1.32.04

#For loop:

螢幕快照 2016-06-27 下午1.52.41

螢幕快照 2016-06-27 下午1.52.53

#While loop:

螢幕快照 2016-06-27 下午1.53.04

#run the conditional suite only when the condition is True

Loop Type Description
while loop Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
nested loops You can use one or more loop inside any another while, for or do..while loop.
Control Statement Description
break statement Terminates the loop statement and transfers execution to the statement immediately following the loop.
continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
pass statement The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

 

# How to run a if / loop function that may raise Errors?

Handling an exception

If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.

Syntax

Here is simple syntax of try….except…else blocks −

try:
   You do your operations here;
   ......................
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   ......................
else:
   If there is no exception then execute this block.
finally:
   This would always be executed.
   ......................

Raising an Exceptions

You can raise exceptions in several ways by using the raise statement. The general syntax for the raise statement is as follows.

Syntax

raise [Exception [, args [, traceback]]]

User-Defined Exceptions

Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions.

Here is an example related to RuntimeError. Here, a class is created that is subclassed from RuntimeError. This is useful when you need to display more specific information when an exception is caught.

In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to create an instance of the class Networkerror.

class Networkerror(RuntimeError):
   def __init__(self, arg):
      self.args = arg

So once you defined above class, you can raise the exception as follows −

try:
   raise Networkerror("Bad hostname")
except Networkerror,e:
   print e.args

Exercise 3:

分別用for,while迴圈各寫⼀個nxn的乘法表 程式 可以讀取使用者輸入的值 n, n>1

輸出樣式: (n=1~9)

螢幕快照 2016-06-27 下午2.32.27 螢幕快照 2016-06-27 下午2.33.29

 


 

Reference:

Coursera

PPT from NTU SPECS course

Tutorialspoint

發表留言