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))

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


版权声明:本文为tttttnl原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。