Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在遇到Python中用户输入的sum后结束程序_Python_Loops_While Loop_Sum - Fatal编程技术网

在遇到Python中用户输入的sum后结束程序

在遇到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

这将提供无限量的求和,但我想在用户输入之前停止求和。您只需比较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 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如果totaltotal = 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))