python数组对每个元素进行计数

2024-10-03 21:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个小程序来计算用户输入的每个零件号。 这就是我能做的

是否有办法将零件号及其频率导出到.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()

Tags: trueinputifdefcounterprintenterchoice
1条回答
网友
1楼 · 发布于 2024-10-03 21:26:05

欢迎来到StackOverflow

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

执行过程如下:

调用mainmanu,它调用createlist,在它完成执行后,它继续执行它离开的指令

 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()

相关问题 更多 >