我想使用另一个函数*PYTHON中的值*

我想使用另一个函数*PYTHON中的值*,python,Python,这是我试图在另一个函数中使用的值 def Task1(): total_titles=0 for each_book in book_list: total_titles = total_titles + float(each_book[5]) print('The total number of book titles available is: ', total_titles) 这是它正在读取的文本文件,标签为book_dat

这是我试图在另一个函数中使用的值

def Task1():

    total_titles=0

    for each_book in book_list:

        total_titles = total_titles + float(each_book[5])
        
    print('The total number of book titles available is: ', total_titles)
这是它正在读取的文本文件,标签为book_data_file.txt

#Listing showing sample book details 
#AUTHOR, TITLE, FORMAT, PUBLISHER, COST?, STOCK, GENRE
P.G. Wodehouse, Right Ho Jeeves, hb, Penguin, 10.99, 5, fiction
A. Pais, Subtle is the Lord, pb, OUP, 12.99, 2, biography
A. Calaprice, The Quotable Einstein, pb, PUP, 7.99, 6, science
M. Faraday, The Chemical History of a Candle, pb, Cherokee, 5.99, 1, science
C. Smith, Energy and Empire, hb, CUP, 60, 1, science
J. Herschel, Popular Lectures, hb, CUP, 25, 1, science
C.S. Lewis, The Screwtape Letters, pb, Fount, 6.99, 16, religion
J.R.R. Tolkein, The Hobbit, pb, Harper Collins, 7.99, 12, fiction
C.S. Lewis, The Four Loves, pb, Fount, 6.99, 7, religion
E. Heisenberg, Inner Exile, hb, Birkhauser, 24.95, 1, biography
G.G. Stokes, Natural Theology, hb, Black, 30, 1, religion
任务的实际问题是选择添加新书项目(我已经成功地完成了,如果上下文需要,我会将代码放在最底部),并提交一份摘要报告,显示(a)库存书籍总数的增加和(b)库存书籍平均价格的成本差异

这是我到目前为止的代码,但我不确定下一步该怎么做,因为我认为我做得不对

def Task4a():

    file = open("book_data_file.txt","r")
    line_count = 0
    for line in book_list:
        if line != "\n":
            line_count += 1
    print('The new total number of titles is: ', line_count)

    print(total_titles)
将值添加到文本文件的代码如下所示

def Task4():

    print('\nEnter the new book record')
    author = input("Enter the author's name: ")
    title = input("Enter the title name: ")
    format_ = input("Enter the format: ")
    publisher = input("Enter the pubisher name: ")
    cost = input("Enter the price: ")
    stock = input("Enter the amount of stock you would like to add: ")
    genre = input("Enter the genre: ")

    bookrecord = [author, title, format_, publisher, cost, stock, genre]
    book_list.append(bookrecord)

有人能帮忙吗?我觉得有一个简单的解决方案,但我真的错过了,谢谢你可以使用return从函数返回值。例如:

def function1(variable1):
   variable2 = variable1 + 1
   return variable1, variable2

variable1, variable2 = function1(2)

可以使用return从函数返回值。例如:

def function1(variable1):
   variable2 = variable1 + 1
   return variable1, variable2

variable1, variable2 = function1(2)

一个函数中的变量在另一个函数中不可用。您应该使用
return
从一个函数返回值,并在另一个函数中使用它们。我建议查阅一个教程来解释它是如何工作的。你能举个例子吗?一个函数中的变量在另一个函数中是不可用的。您应该使用
return
从一个函数返回值,并在另一个函数中使用它们。我建议查阅一个教程来解释它是如何工作的。你能举个例子吗?