【问题标题】:Trying to call a variable from a function to another function试图从一个函数调用一个变量到另一个函数
【发布时间】:2020-08-03 09:12:11
【问题描述】:
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 中的变量来编辑数量,例如,当用户输入一个值时,它会将其添加到文件购物车中,如果您要从文件库存中“减去”,则无法使用 global因为我只会被标记下来。我已经坚持了2天了

【问题讨论】:

  • 了解如何创建minimal reproducible example - 尤其是其中的最小部分。
  • 删除与您从函数中提出的问题无关的所有内容。没有人有时间阅读不相关的代码来回答一个简单的问题。

标签: python function parameters return-value


【解决方案1】:

在第一个函数中写(function name)editselection.(variable name)specificitem=(value)list_of_items[itemselection] 在第二个函数中调用变量,例如: print(editselection.specificitem) 这将打印变量的值。 这被称为函数变量(或类似的东西)

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 2017-11-04
    • 2022-10-24
    • 2018-04-29
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多