2014-11-05 54 views
0

我不知道爲什麼,當我輸入"apathetic"的情緒時,它不起作用。在列表中循環顯示永遠不會看到最後一個元素

''' 
Mood Assessment Application. 
''' 

#function to prompt the user to enter mood 
#and check to see whether the mood entered 
#is valid or not. allowed moods: (happy, sad, angry, apathetic) 
def getMood(message): 
    moods = ['happy', 'sad', 'angry', 'apathetic'] 

    mood = ' ' 
    run = True 
    while run: 
     mood = input("Please enter mood: ") 
     mood = mood.lower() 
     for i in range(len(moods)-1): 
      #print("%s == %s" % (mood, moods[i])) 
      if mood == moods[i]: 
       run = False 
       break 
    return mood 

#function to write mode to the moods.txt 
#file in append mode 
def writeMood(mood): 

    myFile = open("moods.txt", "a") 
    myFile.write(mood + "\n") 
    myFile.close() 

#function to count for mood frequencies 
def moodFrequencies(moods): 
    #['happy', 'sad', 'angry', 'apathetic'] 
    freq = [0, 0, 0, 0] 
    i = 0 
    s = len(moods) - 1 

    #read the moods in reverse order 
    #count last 7 or less 
    while s >= 0 and i < 7: 
     m = moods[s].lower() 
     s -= 1 
     i += 1 
     #print(m) 
     if m == 'happy': 
      freq[0] += 1 
     elif m == 'sad': 
      freq[1] += 1 
     elif m == 'angry': 
      freq[2] += 1 
     else: 
      freq[3] += 1 
    return freq 

#function to load all the moods into the list 
#return the list 
def loadMoods(): 
    myFile = open("moods.txt") 

    moods = [] 

    for line in myFile: 
     moods.append(line.strip()) 

    return moods 

#function to compute the average mood and display it 
def averageMood(f): 
    #['happy', 'sad', 'angry', 'apathetic'] 
    total = (f[0] * 1) + (f[1] * 2) + (f[2] * 3) + (f[3] * 4) 
    avg = int(total/7) 

    if avg == 1: 
     print("You average mood is HAPPY") 
    elif avg == 2: 
     print("You average mood is SAD") 
    elif avg == 3: 
     print("You average mood is ANGRY") 
    else: 
     print("You average mood is APATHETIC") 


#main method 
def main(): 

    run = True 

    #interact with the user and get the input for 
    #mood 
    while run: 
     mood = getMood("Please Enter Your Mood Today: ") 

     #write to the file 
     writeMood(mood) 

     #if the user want to enter more 
     ch = input("\nWould you like to enter another? (y/n): ") 

     #exit loop if he/she don't 
     if ch.lower() == 'n': 
      run = False 

    #load moods 
    moods = loadMoods() 

    #calculate frequencies of the mood read from the file 
    #['happy', 'sad', 'angry', 'apathetic'] 
    freq = moodFrequencies(moods) 

    #average mood 
    averageMood(freq) 
    #print(freq) 
    #mood diagnosis 
    if freq[0] >= 5: 
     print("You are diagnosed as manic") 
    elif freq[1] >= 4: 
     print("You are diagnosed as depressive") 
    elif freq[3] >= 6: 
     print("You are diagnosed as schizoid") 

main() 

回答

4

這是因爲這行:

for i in range(len(moods)-1): 

range返回半開範圍。例如,range(4)爲您提供了四個數字0, 1, 2, 3。所以,range(4-1)給你三個號碼0, 1, 2


同時,值得注意的是,避免斷接一個像這樣的錯誤是你應該直接遍歷序列的原因主要部分。取而代之的是:

for i in range(len(moods)): 
    if mood == moods[i]: 
     # etc. 

...只是這樣做:

for m in moods: 
    if mood == m: 
     # etc. 

或者,如Joran Beasley點出的意見,如果你正在做的唯一的事情是檢查是否mood等於到moods中的任何一個,都可以做得更簡單:

run = mood not in moods 

但是,您可以進一步簡化。你設置一個標誌來打破外層循環,然後break出內層循環,全部讓你可以return。爲什麼不直接使用return

def getMood(message): 
    moods = ['happy', 'sad', 'angry', 'apathetic'] 
    while True: 
     mood = input("Please enter mood: ") 
     mood = mood.lower() 
     if mood in moods: 
      return mood 
+2

或只是'如果mood.lower()在情緒:break' ... + 1所有相同的,那正是他的問題 – 2014-11-05 00:43:18

相關問題