編寫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的值
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息
相關文章