def question1(): addition = 1 + 1 concatenation = '1'+'1' concat_addition = concatenation * addition return(concat_addition) def question2(): output = 0 for number in range(10): output = output + (number % 3) return(output) def asterisk_pattern(spaces,asterisks,spaces_first): if spaces_first: print(spaces*' ',asterisks*'*',sep='',end='') else: print(asterisks*'*',spaces*' ',sep='',end='') def question3(): total = 6 for number in range(total): asterisk_pattern(number,total-number,True) print() def yes_or_no(question): answer = input(question+' Please answer \'yes\' or \'no\': ') if (answer == 'yes') or (answer == 'Yes') \ or (answer == 'Y') or (answer == 'y'): return(True) else: return(False) def classify_the_beast (): print('Use this program to classify a beast.') backbone = yes_or_no('Does the beast have a backbone?') if backbone: feathers = yes_or_no('Does your beast have feathers?') ## birds if feathers: print('Your beast is a bird!') return('bird') else: gills = yes_or_no('Does your beast have gills (at any age)?') if gills: limbs=yes_or_no('Does your beast have limbs with fingers or toes?') if limbs: print('Your beast is an amphibian!') return('amphibian') else: print('Your beast is a fish!') return('fish') else: scaly_skin = yes_or_no('Is its skin scaly or shell-covered?') if scaly_skin: print('Your beast is a reptile!') return('reptile') else: ear_bones = yes_or_no('Does your beast have middle ear bones?') mammary = yes_or_no('Does the female have mammary glands?') hair = yes_or_no('Does your beast have hair?') if ear_bones or mammary or hair: print('Your beast is a mammal!') return('mammal') else: print('Congratulations! You found a new type of vertebrate!') return('Unclassified vertibrate specimen') else: print('Your beast is a slithering slimey crawley thing. ') return('Invertebrate') def question4(): print('Repeat this question for each of the following as per the instructions:') output_list = [] for instruction_list in [['dragon',"Only say 'yes' to backbone and scaly skin"],\ ['unicorn',"Only say 'yes' to backbone, middle ear bones and hair"],\ ['mermaid',"Say 'yes' to backbone, gills and limbs with fingers or toes"], \ ['dust bunnies',"Say no to everything"]]: print('Classify the',instruction_list[0]) print(instruction_list[1]) output_list.append([instruction_list[0],classify_the_beast()]) return(output_list) ######### question 6 with bug fixes ############ def number_one_to_five (attribute): answer = input('Please rate '+attribute+' on a scale from 1 (bad) to 5 (great) :') return(answer) ## return is not properly indented on test def rate_your_sandwich_experience(): score = 0 ## Error on test: not '0' print('Print Thank you for agreeing to complete questionaire about your sandwich experience.') ## Error on test: quotes don't match on test for previous line print('Your results will be Entered into our database, so we can serve you better in the future.') score = score + float(number_one_to_five('freshness of bread')) ## Error on test: No float score = score + float(number_one_to_five('freshness of filling')) score = score + (.5 * (float(number_one_to_five('tastiness of condiments')))) ## error on test: no * operator score = score + (float(number_one_to_five('color of sandwich'))*.25) return(score) ######### question 6 ended ############### ######### question 7 with bug fixes ############ def hat_size(head_circumference): ## error on test: missing colon : import math diameter = head_circumference / math.pi ## error on test: missing "math" in "math.pi" ## hat size is the diameter rounded to the nearest eighth ## error on test: comment above is missing comment markings whole_number = int(diameter // 1) eighths = round((diameter % 1)*8) if eighths == 8: output = str(whole_number + 1) elif eighths == 0: output = str(whole_number) elif eighths == 2: output = str(whole_number)+' 1/4' ## eror on test: missing "str" elif eighths == 4: output = str(whole_number)+' 1/2' elif eighths == 6: output = str(whole_number)+ ' 3/4' ## error on test: hole_number instead of whole_number else: output = str(whole_number)+ ' '+str(eighths)+'/8' print('Your hat size is',output) return(whole_number+(eighths/8)) ######### question 7 ended ####################### def insert_asterisks(sentence): ## extra credit out_string = '' for character in sentence: if character == ' ': out_string = out_string+'*' else: out_string = out_string+character return(out_string) def border_sentence_with_asterisks (sentence,length): padding = length - len(sentence) if (padding%2==0): print((padding//2)*'*',insert_asterisks(sentence),(padding//2)*'*',sep='') else: print(((padding//2)+1)*'*',insert_asterisks(sentence),(padding//2)*'*',sep='') def embed_3_sentences_in_asterisks (sentence1,sentence2,sentence3): length1 = len(sentence1) length2 = len(sentence2) length3 = len(sentence3) length = length1 if length2 > length: length = length2 if length3 > length: length = length3 length = length+6 print(length*'*') border_sentence_with_asterisks(sentence1,length) border_sentence_with_asterisks(sentence2,length) border_sentence_with_asterisks(sentence3,length) print(length*'*') def question8(): embed_3_sentences_in_asterisks('Hello World!','This is a Python print statement','You can recognize it by its syntax') def guess(number): answer = input('I will guess '+str(number)+'. Please answer \'correct\', \'high\' or \'low\' ') if answer == 'correct': return('win') elif answer == 'high' or answer == 'High': return('high') else: return('low') def high_low(high,low,turns): done = False result = '' for turn_so_far in range(turns): if low == high: result = guess(low) done = True elif high == low+1: result = guess(low) if result == 'win': done = True else: low = low+1 else: new_guess = ((low+high)//2) result = guess(new_guess) if result == 'win': done = True elif result == 'high': high = new_guess-1 else: low = new_guess+1 if result == 'win': print('I won in',turn_so_far+1,'turns.') return(turns+1) elif done == True: print('Your answers don\'t make sense. I quit!') print('I lost') def question9(): high_low(1000,1,10)