Reputation: 1
I do not know why, when I enter the "apathetic"
mood, it just does not work.
'''
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()
Upvotes: 0
Views: 90
Reputation: 365767
It's because of this line:
for i in range(len(moods)-1):
range
returns a half-open range. For example, range(4)
gives you the four numbers 0, 1, 2, 3
. So, range(4-1)
gives you the three numbers 0, 1, 2
.
Meanwhile, it's worth noting that avoiding off-by-one errors like this is a major part of the reason you should just be looping over sequences directly. Instead of this:
for i in range(len(moods)):
if mood == moods[i]:
# etc.
… just do this:
for m in moods:
if mood == m:
# etc.
Or, as Joran Beasley points out in the comments, if the only thing you're doing is checking whether mood
is equal to any of moods
, you can do that much more simply:
run = mood not in moods
But you can simplify this even further. You set a flag to break out of the outer loop, then break
out of the inner loop, all so that you can return
. Why not just return
directly?
def getMood(message):
moods = ['happy', 'sad', 'angry', 'apathetic']
while True:
mood = input("Please enter mood: ")
mood = mood.lower()
if mood in moods:
return mood
Upvotes: 4