def sampleA(): output = '1+1' return(output) def minus(total,decrement): ## correction adds colon to previous line output = total - decrement return(output) def sampleB(): return(minus(5,3)) def print_5(): print(5,5,5,sep='*',end='**') print(5,5,5) def question_1(): print_5() def nine_complement(digit): return(9 - digit) def nine_complement_string(number_string): output = '' for digit in number_string: output = output+str(nine_complement(int(digit))) return(output) def question_2(): output = nine_complement_string('0123456789') return(output) def question_3(): output = (float('5'+'0') * 7) ## '5'+'0' contatanates two strings resulting in '50' ## float converts the string '50' to the float 50.0 ## 50.0 X 7 = 350.0 print('output equals:',output) def question_4(): output = 50 for number in range(6): output = output - number ## the loop effectively makes output equal to ## ((((((50 - 0) - 1) - 2) - 3) - 4) - 5) or ## 50 - (0+1+2+3+4+5) = 50 - 15 = 35 print('output equals',output,'after the loop executes') def diagonal_asterisks(length): for spaces in range(length): print(' '*spaces,'*',sep='') def question_5(): diagonal_asterisks(7) ## What will this program return if the input is 'LCXM' (which is not really a ## legal roman numeral) def vending_machine(money, price): if (money < price): print('Not enough money') elif (money == price): print('There is no change.') else: dollars = 0 quarters = 0 dimes = 0 nickels = 0 pennies = 0 change = money - price final_output = change dollars = change // 1 change = change - dollars change = int(change * 100) ## converting to cents ## intial amount of money is in dollars ## for coins smaller than a dollar, ## is is more convenient to calculate ## in terms of cents. if change > 0: quarters = change // 25 change = change - (quarters * 25) if change > 0: dimes = change // 10 change = change - (dimes * 10) if change > 0: nickels = change // 5 change = change - (nickels * 5) pennies = change print('Your change is: ') if dollars > 0: print(dollars,'dollars') if quarters > 0: print(quarters, 'quarters') if dimes > 0: print(dimes, 'dimes') if nickels > 0: print(nickels, 'nickels') if pennies > 0: print(pennies, 'pennies') return(final_output) def question_6(): print('6a') output = vending_machine(5.00,4.34) print('6b. The value of the variable \'output\' is', output) def complain_about(Things): ## This is a corrected version of question 6 output_string1 = 'I really don\'t appreciate '+Things output_string2 = 'I think ' + Things + ' are unfair' print(output_string1) print(output_string2) ### Example with Things set to: tests ### I really don't appreciate tests ### I think tests are unfair. def question_8(): complain_about('tests') def test_foot_to_height_ratio(): ## This is a corrected version of question 7 print('This function calculates and records your foot-length to total height ratio.') print('''All measurements should be given in inches if you are American or in millimeters if you are from anywhere else in the world.''') ### correction of error: triple quotes foot_length=float(input('Please measure your foot and state its length: ')) ### correction of error: hyphen - in 'foot-length' changed to underscore _ height=float(input('Please indicate your total height: ')) ### correction of error -- compare position of float and input functions to original ratio = foot_length / height ### hyphen changed to underscore again print('Thank you for your participation in our study!!!!') print('Your foot-length to height ratio is: ', ratio) print('The average foot-length to height ratio may be about .15') print('Your ratio is ', (ratio / .15), 'times that ratio') ### comma added after (ratio / .15) return(ratio) ## Example print statements given user input of 11 and 72: ## Thank you for your participation in our study!!!! ## Your foot-length to height ratio is: 0.1527777777777778 ## The average foot-length to height ratio may be about .15 ## Your ratio is 1.0185185185185186 times that ratio hline def question_9(): test_foot_to_height_ratio() def dog_sweater(): ## Question 8 sample answer import math print('This program can figure out how much fabric you need to make a dog sweater.') print('Please take out a tape measure and provide the requested information in inches.') print() height = float (input('What is the distance from the base of the dog\'s neck to the base ot its tail? ')) diameter = float (input('What is the height of the thickest portion of the dog\'s chest? ')) print() area = (height * math.pi * diameter) + (2 * math.pi * \ ((diameter / 2) ** 2)) print('You need', area, 'square inches of fabric to make a sweater for your dog.') return(area) def question_10(): dog_sweater() def center_3_sentences(): ## Question 9 sample answer import math print('This program will center 3 lines of text that you provide') sentence1 = input('What is the first line (no more than 65 characters): ') sentence2 = input('What is the second line (no more than 65 characters): ') sentence3 = input('What is the third line (no more than 65 characters): ') offset1 = round((65 - len(sentence1))/ 2) offset2 = round((65 - len(sentence2)) / 2) offset3 = round((65 - len(sentence3)) / 2) print(offset1 * ' ',sentence1,sep='') print(offset2 * ' ',sentence2,sep='') print(offset3 * ' ',sentence3,sep='') def question_11(): center_3_sentences() def yes_or_no(question): answer = '' while not (answer in ['Yes','yes','No','no']): print('Please answer the following question "yes" or "no".') answer = input(question) if answer in ['Yes','yes']: return(True) else: return(False) question1 = 'Do you always share your candy? ' question2 = 'Do you clean up after yourself most of the time? ' question3 = 'Do you give money to charity? ' question4 = 'Do you spend some of your time doing charitable work? ' question5 = 'Do you care about non-human animals? ' question6 = 'Would you commit a crime against another person for personal gain? ' question1a = 'Do you always share your food? ' question2a = 'Do you clean up after other people? ' question3a = 'Would you give up all your money to help a friend in serious need? ' question3a1 = 'Would you do the same for a stranger? ' question4a = 'Would you dedicate your life to a charitable cause and be poor yourself? ' question5a = 'Do you believe in eating all non-human animals? ' question5a1 = 'Do you believe in eating some non-human animals? ' question6a = 'Would you steal so much that your victim becomes poor? ' question6b = 'Would you murder another person for profit? ' def niceness_survey (): SNU = 0 print('This program determines how nice you really are according to') print('the guidelines of the World Council on Niceness (WCN).') print('A final score will be provided in Standard Niceness Units (SNUs)') answer1 = yes_or_no(question1) answer2 = yes_or_no(question2) answer3 = yes_or_no(question3) answer4 = yes_or_no(question4) answer5 = yes_or_no(question5) answer6 = yes_or_no(question6) if answer1: SNU = SNU+10 if yes_or_no(question1a): SNU = SNU+10 if answer2: SNU = SNU+10 if yes_or_no(question2a): SNU = SNU+10 if answer3: SNU = SNU+10 if yes_or_no(question3a): SNU = SNU+10 if yes_or_no(question3a1): SNU = SNU+10 if answer4: SNU = SNU + 10 if yes_or_no(question4a): SNU = SNU + 10 if answer5: SNU = SNU + 10 if yes_or_no(question5a): SNU = SNU - 3 elif not(yes_or_no(question5a1)): SNU = SNU + 10 if answer6: SNU = SNU - 20 if yes_or_no(question6a): SNU = SNU - 30 if yes_or_no(question6b): SNU = SNU - 50 print('Your current niceness is:',SNU,'Standard Niceness Units') return(SNU) def question_12(): niceness_survey()