function - Assign line from .txt file to a variable python for later use -
i have 2 defined functions in both need use line text file found. because 2 different functions can't use "line" in 1 doesn't search text file line. want take value of "line" found , assign variable use in later functions error "variable_line" not defined.
section of first function:
while (len(code) == 8): open('gtin products.txt', 'r') search: line in search: line = line.rstrip('\n') if code in line: variable_line = line #this try assign contents variable print("your search found result: "+line) #this prints content of line add_cart() #second function defined below
second function
def add_cart(): add_cart = "" while add_cart not in ("y", "n"): add_cart = input("would add item cart? ") if add_cart == "y": receipt_list.append(variable_line) #try use variable here error if add_cart == "n": break
make add_cart
accept argument:
def add_cart(variable_line):
you can call this:
add_cart(variable_line) #second function defined below
Comments
Post a Comment