如何解决程序将多个数字相加 3 或 5 但只打印低于其输入的数字
how to solve program that sum of multiple number by 3 or 5 but only print number which is lower than its input
我使用 Python 编写了这样的代码:
a = int(input("number = "))
def solution(a) :
b = 1
c = 0
for b in range (1,a) :
if b%3==0 or b%5==0 :
c += b
print("+",b,end=" ")
if b>=a-1 :
print("=",c)
solution(a)
但是当我运行这段代码时,它显示的是:
number = 10
+ 3 + 5 + 6 + 9 = 23
如何删除数字3前的+?
谢谢
尝试:
a = int(input("number = "))
def solution(a) :
b = 1
c = 0
for b in range (1,a) :
#For first iteration
if c == 0:
if b%3==0 or b%5==0 :
c += b
print(b,end=" ")
#For remaining numbers
if c>b and b%3==0 or b%5==0:
c += b
print("+",b,end=" ")
if b>=a-1 :
print("=",c)
solution(a)
我使用 Python 编写了这样的代码:
a = int(input("number = "))
def solution(a) :
b = 1
c = 0
for b in range (1,a) :
if b%3==0 or b%5==0 :
c += b
print("+",b,end=" ")
if b>=a-1 :
print("=",c)
solution(a)
但是当我运行这段代码时,它显示的是:
number = 10
+ 3 + 5 + 6 + 9 = 23
如何删除数字3前的+?
谢谢
尝试:
a = int(input("number = "))
def solution(a) :
b = 1
c = 0
for b in range (1,a) :
#For first iteration
if c == 0:
if b%3==0 or b%5==0 :
c += b
print(b,end=" ")
#For remaining numbers
if c>b and b%3==0 or b%5==0:
c += b
print("+",b,end=" ")
if b>=a-1 :
print("=",c)
solution(a)