Python 練習實例46

Python 100例 Python 100例

題目:求輸入數字的平方,如果平方運算后小于 50 則退出。

程序分析:

程序源代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

TRUE = 1
FALSE = 0
def SQ(x):
    return x * x
print '如果輸入的數字小于 50,程序將停止運行。'
again = 1
while again:
    num = int(raw_input('Please input number'))
    print '運算結果為 %d' % (SQ(num))
    if num >= 50:
        again = TRUE
    else:
        again = FALSE

以上實例輸出結果為:

如果輸入的數字小于 50,程序將停止運行。
Please input number300
運算結果為 90000
Please input number12
運算結果為 144

Python 100例 Python 100例