哪位是python高手,求解啊。要用while loop。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了哪位是python高手,求解啊。要用while loop。相关的知识,希望对你有一定的参考价值。

a. (8 points) Write a function mult() that prompts the user to enter a number greater
than 0 and then multiplies all the integers between 1 and the number (inclusive)
and prints the result of multiplying all the numbers. Hint: Use a while loop to
iterate through the numbers. Think “count with while”.
b. (2 points) Modify your program so that it only multiplies odd integers.
Here is how it runs:
>>>Enter a number greater than 0: 8
105
Enter a number greater than 0: 20
654729075

mult()是第一问的答案
mult(True)是第二问的答案

def mult(only_odd=False):
while True:
num = raw_input('Enter a number greater than 0: ').strip()
if num and num.isdigit():
num = int(num)
if num > 0:
a = 1
min_num = 2 if only_odd else 1
if only_odd and num % 2 == 0:
num -= 1

while num > min_num:
a *= num
num -= min_num

print a
参考技术A 能不能先翻译下啊。。。E文很费劲。追问

可以. 就是说如果随便输入一个大于0的整数,输出就是那个整数的阶乘。假如我输入5,输出应该是5*4*3*2*1=120

以上是关于哪位是python高手,求解啊。要用while loop。的主要内容,如果未能解决你的问题,请参考以下文章