将数字序列乘以所选数字

时间:2017-09-28 19:23:46

标签: python

如何将1,2,3,4,......中的数字相乘,直到结果高于1000? 我刚刚开始学习Python,但我仍然坚持这个练习。

编辑:是的我在google上做过研究,在我的书中,我尝试了我认为可以工作的东西,但是t get it in the right direction, so I asked here. I check the stackoverflow if there is some similar question, but all I found were slightly different and couldn让我继续前进。

2 个答案:

答案 0 :(得分:1)

# start with a result of 1
result = 1
# and a factor of 1
i = 1

# iterate until the variable result is bigger than 1000
while result <= 1000:
   # increase the factor
   i += 1
   # and multiply the result with this factor
   result *= i
   # print the current state
   print(i, result)

答案 1 :(得分:0)

num1 = 1
num2 = 2
result = 0
while result<1000:
    result = num1 * num2
    print result
    num1 = num1 + 1
    num2 = num2 + 1
print result