这是我已经知道的,不知道从哪里开始。
result = 0
x = int(input('First: '))
y = int(input('Last: '))
y = y+1
for i in range((print(x+1, y)):
print(result)
答案 0 :(得分:2)
这应该有效:
num1 = int(input("Enter first integer: "))
num2 = int(input("Enter second integer: "))
sumNum1Num2N = 0
print("Let's sum all the integers in this range [%d, %d]" %(num1, num2))
#This shows up the sum being done
for i in range(num1, num2 + 1):
sumNum1Num2N = sumNum1Num2N + i
print(sumNum1Num2N, " " , end="")
print()
print("The sum of the numbers between %d and %d is %d" %(num1, num2, sumNum1Num2N))
如果要显示2个数字之间的整数。
for i in range(num1, num2 + 1):
print(i)
但是,如果您想要一种最快的方法来汇总2个数字,请使用内置函数sum
和range
:
m_sum = sum(range(num1, num2 + 1)) #[num1, num2]
在数学上,您也可以实现A和B之间的数字之和,其中B>答:
B*(B + 1)/2 - (A - 1)*A/2
= (B^2 + B - A^2 + A) / 2
= ((B - A)*(B + A) + (B + A)) / 2
= (B + A) * (B - A + 1) / 2
但是我认为这比其他两种方法更复杂,即使数学过程是微不足道的;)
答案 1 :(得分:0)
这可以满足您的需求吗?
x = int(input('First: '))
y = int(input('Last: '))
for i in range(x,y+1):
print(i)
答案 2 :(得分:0)
尝试:
>>> x = int(input('First: '))
First: 1
>>> y = int(input('Last: '))
Last: 10
>>> print(' + '.join(str(e) for e in range(x+1,y)),'=',sum(range(x+1,y)))
2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 44