Python 练习实例46
题目:求输入数字的平方,如果平方运算后小于 50 则退出。
程序分析:无
程序源代码:
实例(Python 2.0+)
#!/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('请输入一个数字:'))
print '运算结果为: %d' % (SQ(num))
if SQ(num) >= 50:
again = TRUE
else:
again = FALSE
以上实例输出结果为:
如果输入的数字小于 50,程序将停止运行。 请输入一个数字:12 运算结果为: 144 请输入一个数字:14 运算结果为: 196 请输入一个数字:1 运算结果为: 1
Python 100例



叮咚
a12***59648z@qq.com
参考解法:
#!/usr/bin/python # -*- coding: UTF-8 -*- while(1): n=input('请输入一个数字:') print '运算结果为: %d' % (n**2) if n**2 < 50: quit() else: print '请继续输入'叮咚
a12***59648z@qq.com
啊香魂
wei***1370@126.com
参考解法:
#! /usr/local/bin # -*- coding:UTF-8 -*- #求输入数字的平方,如果平方运算后小于 50 则退出。 def py_pow( number ): if pow(number,2)<50: print '平方运算后为%2d' % pow(number,2),'结果小于50,所以退出' exit(); else: print '平方运算后为%2d' % pow(number,2),'结果不小于50,所以继续\n' number = int(raw_input('请输入一个数字:\n')) py_pow(number) py_number = int(raw_input('请输入一个数字:\n')) py_pow(py_number)啊香魂
wei***1370@126.com
等一个人
252***465@qq.com
参考方法:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- def power(x): if x ** 2 >= 50: print('{}的平方为:{},不小于50,继续'.format(x, x ** 2)) else: print('{}的平方为:{},小于50,退出'.format(x, x ** 2)) quit() while True: x = int(input('输入数字:')) power(x)等一个人
252***465@qq.com
yorun
547***006@qq.com
参考方法:
#!/usr/bin/python3 while True: x = int(input('input a num :')) x *= x print('结果为:{}'.format(x)) if x > 50: breakyorun
547***006@qq.com
iMax4ever
jia***@foxmail.com
Python3
while True: a = int(input('Enter a number which squr is lager than 50:')) if a * a < 50: print('{}^2 = {}\nit does not meet the demand'.format(a, a * a)) break else: print('{}^2 = {}'.format(a, a * a))iMax4ever
jia***@foxmail.com
段祺晟
475***057@qq.com
python3,lambda 表达式
# 求输入数字的平方,如果平方运算后小于 50 则退出。 s = 100 while s > 50: number = int(input('please enter a number')) f = lambda x: x*x print(f(number))段祺晟
475***057@qq.com