2014-11-18 3 views
0
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, но используя другую функцию. как снова открыть файл в другой функции? Большая часть этого вопроса не имеет отношения к моему вопросу. детали, в которых мне нужна помощь, выделены жирным шрифтом.вызов файла с другой функции

+0

Что именно вы пытаетесь сделать? «открыть файл снова» для чего? читать ? которая является другой функцией? Какой файл вы пытаетесь «открыть снова»? –

+0

Другая функция - def td(): но есть другой код, который не имеет значения. читать так, что я могу получить данные из этого файла – Kelly

+0

. Вы не можете ожидать от нас помощи, вставив только часть вашего кода. Пожалуйста, вставьте весь код. –

ответ

1

изменение

with open(Personaldetails_file) as f: 

в

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

Вы объявили переменную «Personaldetails_file» в первый раз в функции перд(), так что он доступен только из этой функции (переменная сфера является эта функция).

Другой способ: использовать переменную в другом месте, объявить ее вне любой функции в верхней части кода.

+0

thank yooooooou – Kelly

+0

@TessellatingHecker: Указать режим работы файла. –

+0

@ d-coder: как насчет режима работы файла? Он хочет читать, он откроется в режиме чтения и должен работать ...? – TessellatingHeckler

Смежные вопросы