初学者!
我正在尝试制作一个程序,读取X整数并输出(1)所有正数的总和,(2)所有负数的总和,以及(3)所有正数和负数的总和。用户每次都可以以任何不同的顺序输入X号码,并且如果需要可以重复该程序。
为了添加重复功能,我在循环中添加了“break”。但是当我尝试运行它时会显示一个错误,表示“打破外部循环”。我不确定这个错误意味着什么以及如何解决它。
while True:
many = int(input("How many numbers would you like to enter:"))
Sum = 0
sumNeg = 0
for i in range(0,many,1):
num = float(input("Please enter number" +str(i+1)+":"))
Sum = Sum + num
if num < 0:
sumNeg = sumNeg + num
print('Sum =',Sum)
print('sumNeg =', sumNeg)
repeat =input("Would you like to repeat? (Y/N):")
if repeat != 'Y':
break
答案 0 :(得分:0)
break语句在循环之外,因为它的父语句if语句在循环之外。
while True:
many = int(input("How many numbers would you like to enter:"))
Sum = 0
sumNeg = 0
for i in range(0,many,1):
num = float(input("Please enter number" +str(i+1)+":"))
Sum = Sum + num
if num < 0:
sumNeg = sumNeg + num
print('Sum =',Sum)
print('sumNeg =', sumNeg)
repeat =input("Would you like to repeat? (Y/N):")
if repeat != 'Y':
break