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

#-*- 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的值
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息
相关文章