mercredi 22 avril 2015

python - calling up function not working

def td():

choice = input("\nwhich trainning event would you like to access?\n1.swimming\n2.cycling\n3.running\n4.comare data\n5.final eight\nplease type in the number before the event of which you want to choose\n")
if choice == "1": ################################################
    swim()        # will call up a function depending on which option the user choices, the function will take the user to that menu

elif choice == "2":
    cyc()

elif choice == "3":
    run()

elif choice == "4":
    comp()

elif choice == "5":
    eight()

    ######################################################   

def swim():

  Swimming_file_exists = exists("Swimming_file.txt") # this will create a will for swimming as it has 'txt' in it or will get the swimming file that already exsits 
  if  Swimming_file_exists  == False:  # if the file doesnt exsit this will then create it

    Swimming_file = open("Swimming_file","w") # allows user to write to the file 
    Swimming_file.close
    Swimming_file = open("Swimming_file","a")  # open with the "a"  mode the write position will be at the end of the file


  else:
    Swimming_file = open("Swimming_file","a")

    totaldistance = input("what was the total distance you swam in meters?")
    totaltime = input("how long did you swim for in minutes?")
    speed = totaldistance/totaltime # formula for speed so the user doesnt have to work out the speed
    swimtype = input("\nwhich style did you swim\n1.slow freestyle\n2.fast freestyle\n3.backstroke\n4.breaststroke\n5.butterfly\n\nplease type in the number before the swimming style\n") 
    print ("Make sure these are the right values. If not pres ctrl z to undo any mistakes")
    time_hours = totaltime / 60 # this turns the minutes they swam into hours so i am able to calculate the calouries that they have burnt 

    if swimtype == "1":
        calb1 = time_hours * 236 # this is a formula to work out how many calouries the user has burnt for swimming  
    if swimtype == "2":
        calb1 = time_hours * 590
    if swimtype == "3":
        calb1 = time_hours * 413
    if swimtype == "4":
        calb1 = time_hours * 590
    if swimtype == "5":
        calb1 = time_hours * 649


    print ("on average you where swimming at a speed of", speed, "mps\nand you have burnt", clab1,"calories") # spped and calb1 are numbers which the program calculated so 
    score = totaldistance - totaltime # this will make the users score which will show the final 8 
    score_file.write(str(score)) #this will make the score a string and add the score in what thye got to the score file so it can get the final 8 later
    score_file.close()
    total = (totaldistance, totaltime, speed)
    Swimming_file.write(str(total)) # this adds all the swimming details to the file and saves it as a string 
    Swimming_file.close()
    score_file = open("score_file","w") # file for the score will be added and score will be calculated so users are able to see the final 8
    score_file = open("score_file","a")


def run():########################
score_file = open("score_file","w")
score_file = open("score_file","a")

Running_file_e = exists("Running_file.txt") # if the running file already exsits it will open that file
if  Running_file_e  ==False: # if it doesnt exsit this will create a new running file for the user

    Running_file = open("Running_file","w")
    Running_file.close
    Running_file = open("Running_file","a")
else:
    Running_file = open("Running_file","a")


    totaldistanceR = int(input("what was the total distance you ran in KM?"))
    totaltimeR = int(input("how long did you run for in minutes?"))
    print ("Make sure these are the right values.")
    totaltimeR1 = 60/totaltimeR  # this will but the inputed minuted into hours so the program can accuratly calculate the average speed of the runner 
    speedR1 = totaldistanceR/totaltimeR1
    calburn = (speedR1 * 95)/(60/totaltimeR1) # this is an easy formula so the amount of calouries the runner has burnt can be calculated
    score = totaldistance - totaltime
    score_file.write(str(score))
    score_file.close()



    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()



 def cyc():####################################
  score_file = open("score_file","w")
  score_file = open("score_file","a")
  Cycling_file_file_e = exists("Cycling_.txt")
  if  Cycling_file_file_e  ==False:

    Cycling_file_file = open("Cycling_file","w")
    Cycling_file_file.close
    Cycling_file_file = open("Cycling_file","a")

  else:
    Cycling_file = open("Cycling_file","a")

    totaldistancec = int(input("what was the total distance you cycled in KM?"))
    totaltimec = int(input("how long did you cycle for in minutes?"))
    print ("Make sure these are the right values. If not pres ctrl z to undo any mistakes")
    speedc = totaldistancec/totaltimec

    if speedc <10:
        caloriesb= 236 * totaltimec # this works out the calouries for the us4er depending on his input for the distance and time
    if speedc >=10 and speedc <=11.9:
        caloriesb= 354 * totaltimec
    if speedc >=12 and speedc <=13.9:
        caloriesb= 472 * totaltimec
    if speedc >=14 and speedc <=15.9:
        caloriesb= 590 * totaltimec 
    if speedc >=16 and speedc <=20:
        caloriesb= 708 * totaltimec
    if speedc >20:
        caloriesb= 944 * totaltimec

    print ("on average you where running at a speed of", speedc, "KMph\nyou burnt", caloriesb, " calories")
    score = totaldistance - totaltime
    score_file.write(str(score))
    score_file.close()
    totalc = (totaldistancec, totaltimec, speedc)
    Cycling_file.write(str(totalc))
    Cycling_file.close()
    Personaldetails_file.close()



def comp():####################################
Username_file = open ("Username.txt", "r")


Username_file.close()


which_event = input("Which event will you like to compare\n1.swimming\n2.cycling\n3.running\n")
if which_event == "3":
    competitor3 = input("who would you like to compare your data with")
    for n in range (len(ausers)): # this finds the user which the first user is looking for
        x = n + 1
        print (x, ausers[n]) # displays the other usres running details 
        person = ausers[(competitor3 - 1)] #calculates the actual name of the selected user by inputted integer
        run1 = open("Running_file","r") # the "r" is so that the file will only be read 
        line1 = Username_file.readlines()# this reads whole file content into memory 
        run1.close()



if which_event == "2":
    competitor2 = input("who would you like to compare your data with")
    for n in range (len(ausers)): # "(len" is the nembered length of the users 
       x = n + 1 # as python starts counting from 0 this makes it start counting from 1 
       print (x, ausers[n])
       person = ausers[(competitor2 - 1)]
       cyc1 = open("Cycling_file","r")
       line2 = Username_file.readlines()
       cyc1.close()


if which_event == "1":
    competitor1 = input("who would you like to compare your data with")
    for n in range (len(ausers)):
        x = n + 1
        print (x, ausers[n])
        person = ausers[(competitor1 - 1)]
        swim1 = open("Swimming_file","r")
        line1 = Username_file.readlines()
        swim1.close()

for some reason when I type a 1, 2 or 3 the program skips to option 4. I don't know why. it use to work a while ago but I cant remember what I did to make it skip the other functions does anyone know why? thank you

Aucun commentaire:

Enregistrer un commentaire