python数组计算每个元素

时间:2020-05-31 03:05:49

标签: python counter frequency

我想要一个小程序来计算用户输入的每个零件号。 到目前为止,我能做得到。

是否可以将零件编号及其频率导出到.csv文件?

from collections import Counter
thislist = []
frequency = []
partnumber = ""
def mainmanu():
    print ("1. Create List")
    print ("2. Print list")
    print ("3. Exit")
    while True:
        try:
            selection = int (input("Enter Choice: ")
            if selection ==1:
                creatlist(thislist)
            elif selection ==2:
                counteach(thislist)
            elif selection ==3:
                break
    except ValueError:
        print ("invalid Choice. Enter 1-3")
def creatlist(thislist)
   # while True:
        partnumber = input  ("Enter Part number: ")
        if partnumber =="end":
            print(thislist)
            mainmanu()
            break
        thislist.append(partnumber)
def counteach(thislist)
    Counter(thislist)
    mainmanu()

mainmanu()

1 个答案:

答案 0 :(得分:0)

欢迎来到StackOverflow。

您要在另一个由mainmanu函数调用的函数内部调用mainmanu函数。相反,您应该做的是让mainmanu调用所有其他辅助函数。另外,您不能在另一个函数中调用break并期望它在哪里中断。

执行过程如下:

mainmanu被调用,它调用creatlist,完成执行后将继续执行从其离开的指令。

 from collections import Counter
        thislist = []
        frequency = []
        partnumber = ""
        def mainmanu():
            print ("1. Create List")
            print ("2. Print list")
            print ("3. Exit")
            while True:
                try:
                    selection = int (input("Enter Choice: ")
                    if selection ==1:
                        if creatlist(thislist): # line x
                            break
                    elif selection ==2:
                        counteach(thislist)
                    elif selection ==3:
                        break
            except ValueError:
                print ("invalid Choice. Enter 1-3")


        def creatlist(thislist)
           # while True:
                partnumber = input  ("Enter Part number: ")
                if partnumber =="end":
                    print(thislist)
                    return True #this value will make the the condition to be true see line x
                thislist.append(partnumber)
                return false


        def counteach(thislist)
            Counter(thislist)
        mainmanu()