我想用一个小程序来统计用户输入的每一个零件号,这是目前我可以做的。
有什么方法可以将零件号及其频率导出到.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()
欢迎来到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()