2017-07-14 140 views
1

我對python相當陌生。我很難理解如何使用帶有函數的列表。函數將數字從Python中的列表轉換爲輸出

我的程序要求多個參與者並返回輸入參與者數量的信息。

我需要從一個列表中使用函數將秒轉換爲分鐘,但我似乎無法正確工作。

有人可以請解釋我做錯了什麼,並幫助我理解?到目前爲止,我只是試圖將swimTime轉換爲沒有運氣。沒有這個功能,我可以正確地做到。

## This function returns a list of participant last names 
# :return: array of last names 
def getLastNames(): 
    return [ # holds the last name of the participants 
    'Adrian', 'Adserballe', 'Anderson', 'Anderson', 'Anderson', 'Andie', 'Andrews', 'Ardern', 'Arling', 'Arychuk'] 


## This function returns a list of participant first names 
# :return: array of first names 
def getFirstNames(): 
    return [ # holds the last name of the participants 
'Jeff', 'Jacob', 'Julie', 'Jason', 'Micheal', 'Johan', 'Rhonnie','Clover', 'Curtis', 'Darlene'] 

## This function returns a list of the event the participant was in 
# :return: array of events 
def getEvent(): 
    return [ # holds the event id. 1 = standard tri, 2 = sprint tri 
1, 1, 1, 1, 2, 1, 1, 1, 2, 1] 

## This function returns a list of the participant gender 
# :return: array of gender 
def getGender(): 
    return [ # holds the gender of the participant 
    1, 1, 2, 1, 1, 1, 2, 2, 1, 2] 

## This function returns a list of participant divisions 
# :return: array of divisions 
def getDivisions(): 
    return [ # holds the age group assignment for standard and sprint 
'M4044', 'M4549', 'F4044', 'M4044', 'M5054', 'M3539', 'F4549', 'F3034', 'M4549', 'F5559'] 

## This function returns a list of swim times 
# :return: array of swim times 
def getSwimTimes(): 
    return [ # holds the swim times in seconds 
    2026, 1768, 1689, 1845, 2248, 2583, 2162, 1736, 1691, 2413] 

## This function returns a list of transition 1 times 
# :return: array of transition 1 times 
def getT1Times(): 
    return [ # holds the transition I times in seconds 
    329, 224, 131, 259, 271, 264, 205, 164, 127, 285 
] 

## This function returns a list of bike times 
# :return: array of bike times 
def getCycleTimes(): 
    return [ # holds the cycling times in seconds 
    4625, 4221, 4214, 4588, 5440, 5443, 4384, 4710, 4122, 5567] 

## This function returns a list of transition 2 times 
# :return: array of transition 2 times 
def getT2Times(): 
    return [ # holds the transition II times in seconds 
    35, 14, 21, 8, 45, 41, 2, 55, 1, 56] 

## This function returns a list of run times 
# :return: array of run times 
def getRunTimes(): 
    return [ # holds the runTimes in seconds 
    3847, 2882, 2864, 3106, 3835, 4139, 3158, 3477, 2856, 4190 
    ] 

## This function converts seconds to minutes 
# :param: s is the seconds to convert 
# :return: minutes in float format 
def secToMin(s): 
    s = int 
    # Turns seconds into minutes 
    min = s/60 
    # returns number in minutes 
    return min 

## Main entry point of program 
def main(): 
    # declare parallel arrays and populate with data 
    lastName = getLastNames()   # holds the last names of the participants 
    firstName = getFirstNames()   # holds the first names of the participants 
    division = getDivisions()   # holds the age group assignment for standard and sprint 
    swimTimes = getSwimTimes()   # holds the swim times in seconds 
    transition1Times = getT1Times()  # holds the transition I times in seconds 
    cycleTimes = getCycleTimes()  # holds the cycling times in seconds 
    transition2Times = getT2Times()  # holds the transition II times in seconds 
    runTimes = getRunTimes()   # holds the runTimes in seconds 
    event = getEvent()     # holds the event id. 1 = standard tri, 2 = sprint tri 
    gender = getGender()    # holds the gender of the participant 

    numToDisplay = 0     # holds the number of participants to display 
    ttl = 0        # total time accumulator in loop 
    i = 0        # index for the while loop 
    numOfParticipants = 10    # holds the max index number for the arrays 
# Sets Flag 
done = False 
# Executes the loop until done = True 
while done != True: 
    # Gets the number of participants and sets the value in numToDisplay 
    numToDisplay = int(input("\nHow many participants do you wish to display? ")) 
    # Executes a loop until i is equal to the numToDisplay 
    while i != numToDisplay: 
     # Writes "Name: " and the first and last nmae of participant 
     print("\nName: " + firstName[i] + " " + lastName[i]) 
     # Writes "Division: " and" the division number 
     print("Division: ", str(division[i])) 
     # Evaluates the number in event and assigns a type of division 
     if event[i] == 1: 
      # If event number is 1 then Standard Triathlon is assigned to event[i] 
      event[i] = "Standard Triathlon" 
     else: 
      # If event number is not 1 then Sprint triathlon is assigned to event[i] 
      event[i] = "Sprint Triathlon" 
     # Writes Event: " and the event[i]: "1.5km Swim, 40km Bike, 10km Run" 
     print("Event: " + event[i] + ": 1.5km Swim, 40km Bike, 10km Run") 
     # Evaluates the number in gender and assigns the gender to gender[i] 
     if gender[i] == 1: 
      # If gender is equal to 1 then Male is assigned to gender[i] 
      gender[i] = "Male" 
     else: 
      # If gender is not equal to 1 then Female is assigned to gender[i] 
      gender[i] = "Male" 
     # Writes "Gender: " and the gender 
     print("Gender: " + str(gender[i])) 
     # Calls secToMin and assigns the minutes to swimTimes[i] 
     swimTimes[i] = secToMin(min) # I don't think this is right 
     # Writes "Swim: " and dispaly swimtimes as a float with 2 decimal places 
     print("Swim: %3.2f" % swimTimes[i]) 
     # Writes "Transition 1: " and transition1Times[i] and "minutes" 
     print("Transition 1: " + str(transition1Times[i]) + " minutes") 
     # Writes "Bike: " and cycleTimes[i] and "minutes" 
     print("Bike: " + str(cycleTimes[i]) + " minutes") 
     # Writes "Transition 2: " and transition2Times[i] and "minutes" 
     print("Transition 2: " + str(transition2Times[i]) + " minutes") 
     # Writes "Run:" and runTimes[i]) and " minutes" 
     print("Run: " + str(runTimes[i]) + " minutes") 
     # Calculates the total time and assigns it to ttl 
     ttl = swimTimes[i] + transition1Times[i] + cycleTimes[i] + transition2Times[i] + runTimes[i] 
     # Writes "Total: " and ttl and "minutes" 
     print("Total: %3.2f" % ttl + " minutes") 
     # Adds 1 to the index 
     i += 1 
    # Asks if you want to view more participants and assigns the value to more  
    more = input("\nDo you wish to view more participants(Y/N)? ") 
    # If more is equal to yes i is reset back to 0 and done is set equal to False 
    if more == "Y" or more =="y": 
     i = 0 
     done = False 
    else: 
     # If more is equal to no then done is set equal to True 
     if more == "N" or more == "n": 
      done = True 

### call main to run program 
main() 
+1

' s = int'你想要做什麼?是's'一個元素列表?像's = [60]'。如果是這樣,你希望's = s [0]'從列表中取出第0個整數。 – jacoblaw

+0

哪裏是'min'的聲明或賦值?以及你得到什麼類型的問題或錯誤? –

+0

@jacoblaw我試圖從getSwimTimes()中獲取秒數並將其傳遞給secToMin,以便將其轉換爲分鐘數並將該值返回給swimTimes,以便我可以將其顯示爲輸出。我只是困惑於如何正確地做到這一點 – Mike

回答

0

首先s = int不起作用,鑄造你必須使用s = int(s),在這種特定的情況下,你的價值觀是int了。 其次min是python中的保留字,所以你不應該使用它作爲變量。

您可以從列表中通過一個單一的元素,就像使用另一種語言的任何數組:

  swimTimes[i] = secToMin(swimTimes[i]) 

,或者您可以通過/返回整個列表,像這樣:

def getSwimTimes(): 
    return [ # holds the swim times in seconds 
    2026, 1768, 1689, 1845, 2248, 2583, 2162, 1736, 1691, 2413] 


def secToMin(arr): 
    minutes = [] 
    for time in arr: 
     minutes.append(time/60) 
    return minutes 

print secToMin(getSwimTimes()) #prints [33, 29, 28, 30, 37, 43, 36, 28, 28, 40] 
+0

非常感謝。我知道我做錯了什麼,我只是不知道什麼。我一直在尋找答案。 – Mike

相關問題