編寫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的值

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM