Fall 2011 CSCI 220 Function Conditions Solutions

def printTimes(n): strFormat = "%s" + "\t%d" * n   values = list(range(1, n + 1)) values.insert(0, "Times:")

print(strFormat % tuple(values)) for i in range(n): values = [] for j in range(n): values.append((j + 1) * (i + 1)) values.insert(0, str(i + 1)) print(strFormat % tuple(values))

def printTimestable: strFormat = "%s" + "\t%d" * 9 values = list(range(1, 10)) values.insert(0, "Times:")

print(strFormat % tuple(values)) for i in range(9): values = [] for j in range(9): values.append((j + 1) * (i + 1)) values.insert(0, str(i + 1)) print(strFormat % tuple(values))

def badRecord(sentence): highAndMighty = ""

for char in sentence: if (ord('A') <= ord(char) <= ord('Z')): highAndMighty += char

return highAndMighty

def comboLock(num1, num2, num3, num4, num5): message = "You opened the lock."

nums = [num1, num2, num3, num4, num5]

for i in range(len(nums)): p = (i + 1) % 2 np = nums[i] % 2

nums[i] = (nums[i] < 10 and p == np)

print(nums) if (False in nums): message = "You are locked out."

return message

def complimentMaker(answer1, answer2, answer3, answer4): message = "No Comment." compliments = [] if (answer1): compliments.append("super") if (answer2): compliments.append("nice") if (answer3): compliments.append("smart") if (answer4): compliments.append("cool") if (len(compliments) > 0): message = "You are %s." % (" ".join(compliments)) return message

def countDownByThrees(start): for i in range(start, 0, -3): print(i) print("Blast off!")

def xmassTree(x): for i in range(1, x + 2, 2): print("{0:^{1}}".format("*" * i, x))

for i in range(3): print("{0:^{1}}".format("*", x))

def multiplicationTables(number, limit): for i in range(limit + 1): print("%d*%d = %d" % (number, i, number * i))

def checkHeight(height): message = "Sorry. You must be at least 1 meter 32 cm to ride."

if (height >= 132): message = "Have a great ride!"

return message