Home > database >  Ask user to input objects of function after function ran
Ask user to input objects of function after function ran

Time:09-07

i have this code:

import xlrd
c = str(input('word\n'))
d= int(input('column\n'))-1
esme_file = 'D:\python\project1\data1.xlsx'
wb = xlrd.open_workbook(esme_file)
sh = wb.sheet_by_index(0)


def search(a: str = c ,b : int = d):
    ourlist = []
    
    for i in range(sh.nrows):
        ourlist.append(sh.cell_value(i,b))
    print(ourlist.count(a))

as you can see my input objects are out of the function but how can change this to have this function:

if user give us 1 in an input object : start search() then ask for input 'c' and 'd'

then run the function for user

else print("please type 1")

i mean i want somthing like this:

    import xlrd

esme_file = 'D:\python\project1\data1.xlsx'
wb = xlrd.open_workbook(esme_file)
sh = wb.sheet_by_index(0)
    
user_input= ("if you want to search type 1 ")    
def start(g = user_input):
    if g == 1:
        return search()
    else:
        print("please type 1")
        
def search(a: str = c ,b : int = d):
    ourlist = []
    c = str(input('word\n'))
    d = int(input('column\n'))-1
    for i in range(sh.nrows):
        ourlist.append(sh.cell_value(i,b))
        print(ourlist.count(a))

but in this code we have undefined name c and d, how can i solve this? best regards

CodePudding user response:

I'm unsure about what your search function is suppose to do but here is how I would go about it :

If it doesn't answer fully : Give me more details on what the search function is suppose to do and I'll edit my answer.

import xlrd

def search(word , col, filename):
    ourlist = []
    wb = xlrd.open_workbook(filename)
    sh = wb.sheet_by_index(0)
    for i in range(sh.nrows):
        ourlist.append(sh.cell_value(i, col))
        print(ourlist.count(word))

def start():
    choice = input("Type '1' to start a search: ")
    if choice != 1: return None

    word = str(input('Word: '))
    col = int(input('Column: '))-1
    search(word, col, 'D:\python\project1\data1.xlsx')

start()
  • Related