# -*- coding: utf-8 -*-
"""
Created on Mon Jan 14 16:23:52 2019
@author: chen.huaiyu
"""
#1 创建一个函数,用于接收用户输入的数字,并计算用户输入数字的和
from functools import reduce
a_list = []
def add(x, y):
return x+y
try:
while True:
a = input('请输入数字:')
if a == '':
break
else:
a_list.append(int(a))
print(reduce(add, a_list))
except Exception as e:
print('注意:请输入数字 %s' %e)
#2 创建一个函数,传入Ñ个整数,返回其中最大的数和最小的数
b_list = []
def compare(*args):
print('最大值:%s 最小值:%s' %(max(*args), min(*args)))
try:
while True:
b = input('请输入整数:')
if b == '':
break
else:
b_list.append(int)
compare(b_list)
except Exception:
print('注意:请输入数字')
#3 创建一个函数,传入一个参数N,返回ñ的阶乘
def factorial(x):
if x == 1:
return 1
elif x == 0:
return 1
else:
return x*factorial(x-1)
try:
while True:
c = input('请输入数字:')
if c == '':
break
else:
print(factorial(int(c)))
except Exception as e:
print('注意:请输入数字 %s' %e)
===========分割线=============
老师好,对于第三题,解出n的阶乘,虽然写出来并测试正确了,但从一开始就在担心负数的影响,所以走了弯路,但到最后仍然有这样的疑惑,如果老师能看到,烦请解惑,多谢。
展开