我不明白我是如何得到这个错误的,有人可以帮忙:
import time
import os
import xlwt
from datetime import datetime
num = 0
def default():
global num
global model
global partnum
global serialnum
global countryorigin
time.sleep(1)
print ("Model: ")
model = input()
print ()
print ("Part number: ")
partnum = input()
print()
print ("Serial Number: ")
serialnum = input()
print ()
print ("Country of origin: ")
countryorigin = input()
print ("Thanks")
num = num+1
xlwt()
def xlwt():
print ("Do you want to write to excel?")
excel = input()
if excel == "y" or "yes":
excel()
else:
print ("Bye")
sys.exit()
def excel():
print ("Enter a spreadsheet name")
name = input()
wb = xlwt.Workbook()
ws = wb.add_sheet(name)
ws.write(0,0,"Model")
ws.write(0,1,"Part Number")
ws.write(0,2,"Serial Number")
ws.write(0,3,"Country Of Origin")
ws.write(num,0,model)
ws.write(num,1,partnum)
ws.write(num,2,serialnum)
ws.write(num,3,countryorigin)
ws.save(name)
def custom():
print()
def main():
print ("Welcome")
print ()
print ("The deafult catagories are: Model, Part Number, Serial Number,"
"country of origin")
time.sleep(1)
print()
dorc()
def dorc():
print ("Would you like to use the default or custom?")
dorc = input ()
if dorc == "default":
default()
elif dorc == "custom":
custom()
else:
print ("Invalid input")
dorc()
main()
当我执行此操作时,出现错误str object is not callable
。
答案 0 :(得分:1)
您有一个名为excel()
的函数和一个名为excel
的局部变量,您为其指定了一个字符串。
你不能这样做,并期望该功能仍然可用。本地名称excel
会屏蔽全局,因此excel()
会尝试调用input()
返回的字符串结果。
重命名变量:
print ("Do you want to write to excel?")
choice = input()
if choice in ("y", "yes"):
excel()
请注意,我也更正了你的变量测试; excel == "y" or "yes"
没有按照你的想法做,编程语言逻辑与英语语法规则并不完全相同。见Why does `a == b or c or d` always evaluate to True?
接下来,对于导入的模块和函数,使用名称xlwt
会犯同样的错误:
import xlwt
# ...
def xlwt():
# ...
模块和函数都是全局名称,名称只能指向您导入的模块或您创建的函数,而不能同时指向两者。将您的函数重命名为其他内容,否则以下行也将失败:
wb = xlwt.Workbook()
因为xlwt
绑定到您的函数,因为它比模块导入更晚定义。