In [1]:
#练习2
i=0
sum=0
n=int(input("请输入一个正整数作为整数个数"))
while i<n:
    m=int(input("请输入一个正整数,以回车结束"))
    sum=sum+m
    i=i+1
print ('和为',sum)


请输入一个正整数作为整数个数5
请输入一个正整数,以回车结束12
请输入一个正整数,以回车结束3
请输入一个正整数,以回车结束4
请输入一个正整数,以回车结束5
请输入一个正整数,以回车结束6
和为 30

In [2]:
#练习3
import sys
i = 0
a = list()
print("您可以输入“结束”来结束程序")
while True:
    a[i] = input()
    if a[i] == '结束':
        sys.exit(0)
    i += 1


您可以输入“结束”来结束程序
结束
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-30a9d47db636> in <module>()
      4 print("您可以输入“结束”来结束程序")
      5 while True:
----> 6     a[i] = input()
      7     if a[i] == '结束':
      8         sys.exit(0)

IndexError: list assignment index out of range

In [6]:
#练习4
import sys
sum = 0
factor = 1
while True:
    n = input()
    n = int(n)
    if (sum_1 < n) and (sum_2 < (n*n)) :
        sys.exit(0)
    sum += n
    factor *= n


1
1
1
6
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0
G:\Python\lib\site-packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

In [ ]:


In [ ]:


In [ ]:


In [ ]: