从另一个函数调用文件

2024-09-29 02:15:31 发布

您现在位置:Python中文网/ 问答频道 /正文

def file():
    Username_file = open("Username_file.txt", "w")
    Username_file.close() 
    Username_file = open ("Username_file.txt", "a")
    Firstname = input("Firstname:")
    Lastname = input("Lastname:")

    fullname = (Firstname, Lastname)

    Username_file.write(str(fullname)) 
    Username_file.close()

def perd():

    **Personaldetails_file = open("Personaldetails_file.txt", "w")
    Personaldetails_file = open("Personaldetails_file.txt", "a")**

    pd = input("are you a new user?")

    if pd == "yes":
         print ("add your details")

    age = int(input("how old are you?"))
    DOB = input("Date of birth:")
    gender = input("Gender:")
    weight = int(input("weight in kg:"))
    height = int(input("height in cm:"))  

    Personaldetails = (age, DOB, gender, weight, height) 

    Personaldetails_file.write(str(Personaldetails))
    Personaldetails_file.close()  

def td(): 

    choice = input("which trainning event would you like to access?\n1.swimming\n2.cycling\n3.running\nplease type in the number before the event of which you want to choose\n")
    if choice == "1":
        Swimming_file= open("Swimming_file.txt", "w")

        totaldistance = input("what was the total distance you swam in meters?")
        totaltime = input("how long did you swim for in minutes?")
        speed = totaldistance/totaltime
        print ("on average you where running at a speed of", speed, "mps\nyou can look at the tables to see how many calouries you have burnt")

        total = (totaldistance, totaltime, speed)
        Swimming_file.write(str(total))
        Swimming_file.close() 

    elif choice == "3":
        Running_file= open("Running_file.txt", "w")


        totaldistanceR = int(input("what was the total distance you ran in KM?"))
        totaltimeR = int(input("how long did you run for in minutes?"))
        totaltimeR1 = 60/totaltimeR
        speedR1 = totaldistanceR/totaltimeR1
        calburn = (speedR1 * 95)/(60/totaltimeR1)

        print ("The records have been saved")
        print ("on average you where running at a speed of", speedR1, "KMph\nyou burnt",calburn," calouries")

        totalR = (totaldistanceR, totaltimeR, speedR1, calburn)
        Running_file.write(str(totalR))
        Running_file.close()

    elif choice == "2":
        Cycling_file= open("Cycling_file.txt", "w")
        **with open(Personaldetails_file) as f:
            content = [x.strip('\n') for x in f.readlines()]**
            age = lines[0]
            weight = lines [3]
            height = lines [4]    
        totaldistancec = int(input("what was the total distance you cycled in KM?"))
        totaltimec = int(input("how long did you cycle for in minutes?"))
        calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age)                 
        speedc = totaldistancec/totaltimec
        print ("on average you where running at a speed of", speedc, "KMph\nyou burnt", calburn1, " calouries")

        totalc = (totaldistancec, totaltimec, speedc)
        Cycling_file.write(str(totalc))
        Cycling_file.close()
        Personaldetails_file.close()

这是我的大部分程序。 我需要打开一个用python创建的文件,但使用不同的函数。如何在另一个函数中再次打开该文件? 大部分与我的问题无关。我需要帮助的部分是粗体的


Tags: theintxtyoucloseinputusernameopen
1条回答
网友
1楼 · 发布于 2024-09-29 02:15:31

改变

with open(Personaldetails_file) as f:

with open("Personaldetails_file.txt") as f:

您第一次在perd()函数中声明了变量“Personaldetails\u file”,因此只能从该函数访问它(变量的作用域就是该函数)

另一种方法是在其他地方使用变量,在代码顶部的任何函数之外声明它

相关问题 更多 >