在Python中0!= 9和0!= 10?

时间:2019-03-22 07:49:10

标签: python output

我需要编写一个函数,使用while循环从头到尾(不包括最后一个)计算整数的数量。我一直得到0!= 9和0!= 10的输出。 代码:

n = 0
k = 1

def div_3_5(start, end):
    global n
    return n
    while (n<k):
        print("Divisible by 3 or 5") 
        if n % 3 == 0 or n % 5 == 0:
            n +=1 

我已经坚持了一段时间,请有人帮忙

1 个答案:

答案 0 :(得分:0)

您几乎在函数的顶部都有return n语句,结果该程序永远不会执行循环

n = 0
k = 5

def div_3_5(start, end):
   flag = False
   number = start
   count = 0
   while flag is False:
      if number % 3 == 0 or number % 5 == 0:
         print("Divisible by 3 or 5")
         print(number)
         count += 1  # So you can see how many times the condition is met

      if number == end:
         flag = True

      number += 1
   print(count)
   return count

div_3_5(n, k)