Попытка вызвать переменную из функции в другую функцию - PullRequest
0 голосов
/ 03 августа 2020
def editselection():
  #this converts the text in the files into a list in a list
  with open("stocks", "r") as stocks:
    for line in stocks:
      stripped_line = line.strip()
      line_list = stripped_line.split()
      list_of_items.append(line_list)
    itemselection = input('Choice: ')
    if itemselection.isalpha() == True:
      ManageStock()
    elif itemselection == '':
      ManageStock()
  itemselection = int(itemselection)
  os.system('clear')
  #the square brackets are the indexes so for example if they select 0, the first item turned into a list would be known as specific item
  specificitem = list_of_items[itemselection]
  changeitem(specificitem)
  return specificitem

Я пытаюсь вызвать переменную 'specificitem' в функцию AddItem ()

def AddToCart(specificitem): 
  os.system('clear')
  number = 0
  os.system('clear')
  print ("""Here is the current stock 
--------------------------
Name, Price, Quantity
--------------------------
""")
  with open ('stocks', 'r') as stocks:
    for i in stocks:
      number = str(number)
      print (number+'.' , i)
      number = int(number)
      number = number + 1
  #this converts the text in the files into a list in a list
  with open("stocks", "r") as stocks:
    for line in stocks:
      stripped_line = line.strip()
      line_list = stripped_line.split()
      list_of_items.append(line_list)
    itemselection = input('Choice: ')
    if itemselection.isalpha() == True:
      AddToCart()
    if itemselection == '':
      MakeASale()
  itemselection = int(itemselection)
  #the square brackets are the indexes so for example if they select 0, the first item turned into a list would be known as specific item
  quantity = input('How many would you like? ')
  chosenitem2 = list_of_items[itemselection]
  with open ('cart' , 'a') as cart:
    chosenitem2 = str(chosenitem2)
    cart.write(chosenitem2 + '\n')
  with open("cart", "r") as cart:
    for line in cart:
      stripped_line = line.strip()
      line_list = stripped_line.split()
      list_of_cart.append(line_list)
    with open ("cart" , "r+") as cart:
      data = cart.read()
      data = data.replace(chosenitem2[2], quantity) 
      cart.close
      cart = open('cart' , 'wt')
      cart.write(data)
      cart.close()
    with open ("stocks" , "r+") as stocks:
      data = stocks.read()
      data = data.replace(specificitem[2], chosenitem2[2]) 
      stocks.close
      stocks = open('stocks' , 'wt')
      stocks.write(data)
      stocks.close()
    print(chosenitem2)

, хотя в AddToCart () отсутствует 1 обязательный позиционный аргумент: 'specificitem'

Я пытаюсь использовать переменную из editselection для редактирования количества, например, когда пользователь вводит значение, оно добавляет его в корзину с файлами и «вычитает», если хотите, из запасов файлов, использование глобального недоступен из-за того, что меня просто пометят. Я застрял на этом уже 2 дня

1 Ответ

0 голосов
/ 03 августа 2020

В первой функции напишите (function name)editselection.(variable name)specificitem=(value)list_of_items[itemselection] А во второй функции вызовите переменную, например, так: print(editselection.specificitem) И это напечатает значение переменной. Это называется функциональной переменной (или что-то вроде этого)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...