列表中的无限循环和字符串比较
我是一名新程序员,学习python。我已经写了这个代码,但我需要帮助1.做一个无限循环,这样用户可以在列表中输入许多数字。2.规定无限循环结束,比如键入"完成"一词。下面是代码:
选择 | 换行 | 行号
- print "Welcome to average script "
- def main():
- #declaration of an empty list avg
- avg = []
- loop = 1
- while loop ==1:
- try:
- avg.append( int(raw_input("Enter the first number you want average: \n")))
- avg.append( int(raw_input("Enter the second number you want average: \n")))
- avg.append( int(raw_input("Enter the third number you want average: \n")))
- print "The list is: ", avg
- ambo = len(avg)
- print "The number of members in the list is: ", ambo
- s = sum(avg)
- print "The sum of the enterd numbers is: ",s
- a = float (s/ambo)
- print "The average of the enterd members of the list are: ", a
- break
- except ValueError:
- print "\n Please Enter a number (Integer)"
- try:
- loop = input("Press 1 to try again: ")
- avg = []
- except NameError:
- loop = 1
- continue
- main()