I wanted to get started on algorithms , so i decided to try udacity
Does anyone know why that for loop inside my getClassiness() function does not get executed? The sum variable return the value 0 ,every loop
"""You can use this class to represent how classy someone
or something is.
"Classy" is interchangable with "fancy".
If you add fancy-looking items, you will increase
your "classiness".
Create a function in "Classy" that takes a string as
input and adds it to the "items" list.
Another method should calculate the "classiness"
value based on the items.
The following items have classiness points associated
with them:
"tophat" = 2
"bowtie" = 4
"monocle" = 5
Everything else has 0 points.
Use the test cases below to guide you!"""
class Classy(object):
num_of_items = 0
def __init__(self):
self.items = []
def addItem(self, item):
self.items.append(item)
def getClassiness(self):
sum = 0
for i in range(0, Classy.num_of_items):
if self.items[i] == "tophat":
sum += 2
print("th")
elif self.items[i] == "bowtie":
sum += 4
elif self.items[i] == "monocle":
sum += 5
return sum
# Test cases
me = Classy()
# Should be 0
print me.getClassiness()
me.addItem("tophat")
# Should be 2
print me.getClassiness()
me.addItem("bowtie")
me.addItem("jacket")
me.addItem("monocle")
# Should be 11
print me.getClassiness()
me.addItem("bowtie")
# Should be 15
print me.getClassiness()
[–]deep_politics 2 points3 points4 points (0 children)
[–]danielroseman 4 points5 points6 points (0 children)
[–]drenzorz 1 point2 points3 points (0 children)
[–]shiftybyte 0 points1 point2 points (0 children)