-3

I don't understand how I am getting this error, can someone help:

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

When I execute this, I get an error str object is not callable.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253

1 Answers1

1

You have both a function named excel() and a local variable named excel that you assigned a string to.

You can't do that and expect the function to still be available. The local name excel masks the global, so excel() tries to call the string result that input() returned.

Rename your variable:

print ("Do you want to write to excel?")
choice = input()
if choice in ("y", "yes"):
    excel()

Note that I also corrected your variable test; excel == "y" or "yes" does not do what you think it does, programming language logic is not quite the same as English grammar rules. See Why does `a == b or c or d` always evaluate to True?

Next, you make the same mistake by using the name xlwt for both a module you import and a function:

import xlwt

# ...

def xlwt():
    # ...

Both the module and the function are global names, and the name can only point to either the module you imported or the function you created, not both at the same time. Rename your function to something else, otherwise the following line will fail too:

wb = xlwt.Workbook()

because xlwt is bound to your function as that was defined later than the module import.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I have fixed that now thanks: what is this error ", line 43, in excel wb = xlwt.Workbook() AttributeError: 'function' object has no attribute 'Workbook' – Leo Rogers Jul 13 '16 at 14:20
  • @LeoRogers: every name in a Python program lives in the same namespace. `xlwt` can't both be a module you imported and a function you created. Rename your function. – Martijn Pieters Jul 13 '16 at 14:27