Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 46

Python Exercise Example 46

Python 100 Examples Python 100 Examples

**Problem:** Calculate the square of an input number; if the squared result is less than 50, exit.

**Program analysis:** None

Program source code:

Example

#!/usr/bin/python

-- coding: UTF-8 --

TRUE

=

1

FALSE

=

0

def

SQ

(

x

)

:

return

x

x

print

(

'

If the input number is less than 50, the program will stop running.

'

)

again

=

1

while

again

:

num

=

int

(

input

(

'

Please enter a number:

'

)

)

print

(

'

The result is: %d

'

%

(

SQ

(

num

)

)

)

if

SQ

(

num

)

=

50

:

again

=

TRUE

else

:

again

=

FALSE

The output of the above example is:


如果输入的数字小于 50,程序将停止运行。
请输入一个数字:12
运算结果为: 144
请输入一个数字:14
运算结果为: 196
请输入一个数字:1
运算结果为: 1

Python 100 Examples Python 100 Examples