python练习题5.26

1.

"""Question:
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
between 2000 and 3200 (both included).
The numbers obtained should be printed in a comma-separated sequence on a single line."""

a=list(range(2000,3201,1))
number_need=[]
for number in a:
	number_7=number%7
	number_5=number%5
	if number_7 == 0 and number_5 != 0:
		number_need.append(number)
print(number_need)

总结:1.其中 range函数输出仍然显示range,因为range()函数返回的是生成器对象
生成器对象直接打印出不来内容,只会返回对象信息。想要看生成器具体会产生什么,可以使用list()、或者tuple()函数转换。
2.一开始打算用type函数通过判断数据类型的方式解题,但是数据类型与是否除尽无关
3.注意%,//的用法

2.

"""Question:
Write a program which can compute the factorial of a given numbers.
The results should be printed in a comma-separated sequence on a single line.
Suppose the following input is supplied to the program:
8
Then, the output should be:
40320"""
"""第一种方法"""
number=input("number")
number=int(number)
l=list(range(1,number+1))
z=1
for n in l:
	y=n*z
	z=y
print(z)
input()
"""第二种方法嵌套函数"""
def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)


print (fact(8))

总结:第二种方法用的嵌套函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值