编写python程序读入1到100之间的整数,然后计算每个数出现的次数,输入0表示结束输人,输入数据不包括0。如果数出现的大现如果大于1,输出时使用复数times

#user/bin/python
#-*- coding:UTF-8 -*-
#环境:python3

print("Enter the numbers between 1 and 100:")
enterList=[] #记录输入的元素
while 1:
    a = int(input(">>>"))  #将输入转换为int型
    if a == 0:
        print ("Your input:",enterList)
        break  #结束输入
    if a<1 or a>100:  #限制只允许输入1到100之间的数
        print("Error!Please enter the numbers between 1 and 100!")
    else:
        enterList.append(a)
listVisited=[]  #保存列表中已经处理过的元素值,避免相同的值处理多次
for a in enterList:
    if a in listVisited: #已经处理过a,跳过此次循环
        break
    if enterList.count(a)>1:
        print("The number",a,"occurs",enterList.count(a),"times!")
    if enterList.count(a)==1:
        print"The number", a, "occurs",enterList.count(a), "time!"
    listVisited.append(a) #处理完a,把a 保存到这个列表中,以便下次跳过a的值
 

猜你喜欢

转载自www.cnblogs.com/cxl-blog/p/9971880.html