在遇到Python中用户输入的sum后结束程序
这将提供无限量的求和,但我想在用户输入之前停止求和。您只需比较while循环的条件语句中的两个值,如下所示:在遇到Python中用户输入的sum后结束程序,python,loops,while-loop,sum,Python,Loops,While Loop,Sum,这将提供无限量的求和,但我想在用户输入之前停止求和。您只需比较while循环的条件语句中的两个值,如下所示: total = 0 not_total = True sum_of_square = int(input("Enter desired sum: ")) while (not_total == True): for n in range (2): total = total + n**2 print("The sum of square
total = 0
not_total = True
sum_of_square = int(input("Enter desired sum: "))
while (not_total == True):
for n in range (2):
total = total + n**2
print("The sum of squares from 1^2 to {0}^2 is equal to {1}.".format(n,total))
我尝试了那段代码,但它作为一个无限循环返回,但我尝试了这样一个if语句。。。total=0 not_total=True sum_of_square=intinpunter期望的和:而not_total==True:对于范围1100中的n,1:total=total+n**2如果total
total = 0
sum_of_square = int(input("Enter desired sum: "))
while (sum_of_squares <= total):
for n in range (2):
total = total + n**2
print("The sum of squares from 1^2 to {0}^2 is equal to {1}.".format(n,total))