Python 循环控制练习
Python Loop Controls Exercise
过去几周我才开始学习 python 超技能。
场景如下:
Write a program that reads from the console integers (one in a line) until their sum is equal to 0. Immediately after that, it should display the sum of the squares of all the entered numbers.
It is guaranteed that at some point the sum of the entered numbers will be equal to 0. After that, reading is not necessary to continue.
In case the first integer equals to 0, also stop reading values from the input. Print out 0 instead of the sum of the squares.
For example, we are reading the numbers 1, -3, 5, -6, -10, 13. At this point, we have noticed that the sum of these numbers is 0 and output the sum of their squares, not paying attention to the fact that there are still unread values.
num = int()
listtrigbreak = []
listsquares = []
sumtrig = 0
sumsqua = 0
while sumtrig != 0: # until sum of trig is not 0,
num = int(input()) #accept numbers
if num == "0": # while first input is 0,
print(0) # print 0
break # and break the loop
listtrigbreak.append(num) # append num to listtrig
sumtrig += sum(listtrigbreak) # add sum of list to sumtrig
for x in listtrigbreak: # for each number in listtrigbreak
squared = x ** 2 # convert each to squares, save variable
listsquares.append(squared) # add squared to listsquq
sumsqua = sum(listsquares) # sum of squares in listsqua
else:
print(sumsqua)
我什至无法通过第一个 while 循环。每当我 运行 它时,它都会跳过整个 while 循环并转到这个:
else:
print(sumsqua)
我从一开始就很难使用布尔逻辑。我需要解释。
正如其他评论已经指出的那样,while 循环有点棘手,因为您最初将变量声明为 0。您可以使用控制 while 循环的布尔开关,如下所示:
input_integers = []
stop = False
while not stop: # So while we should keep asking user input...
num = int(input())
input_integers.append(num)
if sum(input_integers) == 0 and len(input_integers) > 0:
print('done')
stop = True
现在在其中添加其他功能,您就完成了。
过去几周我才开始学习 python 超技能。
场景如下:
Write a program that reads from the console integers (one in a line) until their sum is equal to 0. Immediately after that, it should display the sum of the squares of all the entered numbers. It is guaranteed that at some point the sum of the entered numbers will be equal to 0. After that, reading is not necessary to continue.
In case the first integer equals to 0, also stop reading values from the input. Print out 0 instead of the sum of the squares.
For example, we are reading the numbers 1, -3, 5, -6, -10, 13. At this point, we have noticed that the sum of these numbers is 0 and output the sum of their squares, not paying attention to the fact that there are still unread values.
num = int()
listtrigbreak = []
listsquares = []
sumtrig = 0
sumsqua = 0
while sumtrig != 0: # until sum of trig is not 0,
num = int(input()) #accept numbers
if num == "0": # while first input is 0,
print(0) # print 0
break # and break the loop
listtrigbreak.append(num) # append num to listtrig
sumtrig += sum(listtrigbreak) # add sum of list to sumtrig
for x in listtrigbreak: # for each number in listtrigbreak
squared = x ** 2 # convert each to squares, save variable
listsquares.append(squared) # add squared to listsquq
sumsqua = sum(listsquares) # sum of squares in listsqua
else:
print(sumsqua)
我什至无法通过第一个 while 循环。每当我 运行 它时,它都会跳过整个 while 循环并转到这个:
else:
print(sumsqua)
我从一开始就很难使用布尔逻辑。我需要解释。
正如其他评论已经指出的那样,while 循环有点棘手,因为您最初将变量声明为 0。您可以使用控制 while 循环的布尔开关,如下所示:
input_integers = []
stop = False
while not stop: # So while we should keep asking user input...
num = int(input())
input_integers.append(num)
if sum(input_integers) == 0 and len(input_integers) > 0:
print('done')
stop = True
现在在其中添加其他功能,您就完成了。