Python代写-COMP1021
时间:2021-10-26

COMP1021 Introduction to Computer Science Fall 2020 Midterm Exam Part A Questions (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 1 You have a summer job at Subway Sandwiches. You make a program to help you keep track of the jobs you have to do each day. list_of_jobs =["clean floor", "bake bread", \ "check vegetables", "make cookies", \ "clean oven", "wipe glass"] while len(list_of_jobs) > 0: print() for index in range( len(list_of_jobs) ): print(index, list_of_jobs[ index ]) print("Which job have you done?") this_job_index = input("Enter one of the numbers: ") this_job_index = int(this_job_index) list_of_jobs.remove(list_of_jobs[ this_job_index ]) print() print("Finished all jobs!") ? ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. list_of_jobs =["clean floor", "bake bread", \ "check vegetables", "make cookies", \ "clean oven", "wipe glass"] while len(list_of_jobs) > 0: print() for index in range( len(list_of_jobs) ): print(index, list_of_jobs[ index ]) print("Which job have you done?") this_job_index = input("Enter one of the numbers: ") this_job_index = int(this_job_index) list_of_jobs.remove(list_of_jobs[ this_job_index ]) print() print("Finished all jobs!") Part A Question 1 - Answer You have a summer job at Subway Sandwiches. You make a program to help you keep track of the jobs you have to do each day. input() produces text, so we need to convert the text to an integer Two possible answers: > (is greater than) != (is not equal to) Also OK: this_job_index=int(float(this_job_index)) (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 2 When the following program is executed the user enters two numbers (i.e.m and n). Based on the input two values, which one of the following images cannot be generated by the program? import turtle turtle.speed(0) turtle.hideturtle() m = int(input("Enter an integer m:")) n = int(input("Enter an integer n:")) for _ in range(m): for _ in range(n): turtle.forward(30) turtle.right(360 / n) turtle.up() turtle.forward(50) turtle.right(360 / m) turtle.down() turtle.done() (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 2 - Answer When the following program is executed the user enters two numbers (i.e.m and n). Based on the input two values, which one of the following images cannot be generated by the program? import turtle turtle.speed(0) turtle.hideturtle() m = int(input("Enter an integer m:")) n = int(input("Enter an integer n:")) for _ in range(m): for _ in range(n): turtle.forward(30) turtle.right(360 / n) turtle.up() turtle.forward(50) turtle.right(360 / m) turtle.down() turtle.done() cannot m=3, n=3 m=4, n=4 m=5, n=3 m=5, n=5 m=6, n=4 turtle.forward(50) makes the shapes not touch at the center (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 3 Fill in the blank so that the number of teachers in the COMP1021 teaching team is correctly counted. Here is a Python program: teaching_team = [ ("Dave", "Teacher"), ("Peter", "TA"), ("Jimmy", "TA"), ("Cecia", "Teacher"), ("Gibson", "Teacher"), ("Kelvin", "TA"), ("Alex", "Teacher"), ("Dimitris", "Teacher") ] count_teacher = 0 for t in teaching_team: if t[ 1 ] == "Teacher": count_teacher = count_teacher + 1 print("count_teacher:", count_teacher) ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 3 - Answer Fill in the blank so that the number of teachers in the COMP1021 teaching team is correctly counted. Here is a Python program: teaching_team = [ ("Dave", "Teacher"), ("Peter", "TA"), ("Jimmy", "TA"), ("Cecia", "Teacher"), ("Gibson", "Teacher"), ("Kelvin", "TA"), ("Alex", "Teacher"), ("Dimitris", "Teacher") ] count_teacher = 0 for t in teaching_team: if t[ 1 ] == "Teacher": count_teacher = count_teacher + 1 print("count_teacher:", count_teacher) The program prints this: (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 4 What is the missing piece of code? There may be more than one answer. However, the code you write must be the simplest (least typing and least complex) code that works. Write the missing piece of code. Don’t write the whole line of code, just write the missing part. Don’t write any spaces in your answer. import turtle turtle.speed(0) turtle.bgcolor("white") turtle.hideturtle() diameter=400 for value in range(100): turtle.color( ) turtle.dot(diameter) diameter= diameter-4 turtle.done() ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 4 import turtle turtle.speed(0) turtle.bgcolor("white") turtle.hideturtle() diameter=400 for value in range(100): turtle.color( ) turtle.dot(diameter) diameter= diameter-4 turtle.done() ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 4 - Answer What is the missing piece of code? There may be more than one answer. However, the code you write must be the simplest (least typing and least complex) code that works. Write the missing piece of code. Don’t write the whole line of code, just write the missing part. Don’t write any spaces in your answer. import turtle turtle.speed(0) turtle.bgcolor("white") turtle.hideturtle() diameter=400 for value in range(100): turtle.color( ) turtle.dot(diameter) diameter= diameter-4 turtle.done() Accepted answers: "gray"+str(value) 'gray'+str(value) "grey"+str(value) 'grey'+str(value) ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 5 A student is practicing Python programming by writing a function. When the function is run it randomly chooses and displays one of the 5 days of the week, Monday to Friday. (Saturday and Sunday are not included). You can assume import random has been executed before these functions are executed. Which function is NOT suitable/correct? def ChooseDay(): if random.randint(1, 5)==1: print("Monday") elif random.randint(1, 5)==2: print("Tuesday") elif random.randint(1, 5)==3: print("Wednesday") elif random.randint(1, 5)==4: print("Thursday") elif random.randint(1, 5)==5: print("Friday") def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] number=random.randint(0, 4) print(days[number]) def ChooseDay(): day = random.randint(1, 5) if day == 1: print("Monday") elif day == 2: print("Tuesday") elif day == 3: print("Wednesday") elif day == 4: print("Thursday") elif day == 5: print("Friday") def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] print(random.choice(days)) def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] numbers=[0,1,2,3,4] thisnumber=random.choice(numbers) print(days[thisnumber]) (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 5 - Answer A student is practicing Python programming by writing a function. When the function is run it randomly chooses and displays one of the 5 days of the week, Monday to Friday. (Saturday and Sunday are not included). You can assume import random has been executed before these functions are executed. Which function is NOT suitable/correct? def ChooseDay(): if random.randint(1, 5)==1: print("Monday") elif random.randint(1, 5)==2: print("Tuesday") elif random.randint(1, 5)==3: print("Wednesday") elif random.randint(1, 5)==4: print("Thursday") elif random.randint(1, 5)==5: print("Friday") def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] number=random.randint(0, 4) print(days[number]) def ChooseDay(): day = random.randint(1, 5) if day == 1: print("Monday") elif day == 2: print("Tuesday") elif day == 3: print("Wednesday") elif day == 4: print("Thursday") elif day == 5: print("Friday") def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] print(random.choice(days)) def ChooseDay(): days=["Monday","Tuesday","Wednesday", \ "Thursday","Friday"] numbers=[0,1,2,3,4] thisnumber=random.choice(numbers) print(days[thisnumber]) correct correct correct correct This function is not correct Sometimes this function will print nothing! (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 5 - Incorrect Function def ChooseDay(): if random.randint(1, 5)==1: print("Monday") elif random.randint(1, 5)==2: print("Tuesday") elif random.randint(1, 5)==3: print("Wednesday") elif random.randint(1, 5)==4: print("Thursday") elif random.randint(1, 5)==5: print("Friday") 3 is not equal to 1 5 is not equal to 2 1 is not equal to 3 The random number is 3 2 is not equal to 4 4 is not equal to 5 The random number is 5 The random number is 1 The random number is 2 The random number is 4 Nothing is printed Nothing is printed Nothing is printed Nothing is printed Nothing is printed So in this situation, nothing is printed (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 6 After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle turtle.left(90) for d in range(2): for i in range(2700): turtle.forward(0.1) if d: turtle.right(0.1) else: turtle.left(0.1) turtle.done() (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 6 – Speeding Up The Program After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle turtle.left(90) for d in range(2): for i in range(2700): turtle.forward(0.1) if d: turtle.right(0.1) else: turtle.left(0.1) turtle.done() This program takes 5 minutes to run! How to speed it up? Possibility 1: add turtle.speed(0) After doing that, it still takes 4 minutes to run! (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle turtle.left(90) for d in range(2): for i in range(2700): turtle.forward(0.1) if d: turtle.right(0.1) else: turtle.left(0.1) turtle.done() This program takes 5 minutes to run! How to speed it up? Possibility 2: add turtle.tracer(False) and turtle.tracer(True) Then the result is immediately displayed! Part A Question 6 – Speeding Up The Program (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle turtle.left(90) for d in range(2): for i in range(2700): turtle.forward(0.1) if d: turtle.right(0.1) else: turtle.left(0.1) turtle.done() This program takes 5 minutes to run! How to speed it up? Possibility 3: reduce the number of loops by 10 and increase the distance travelled by 10 After doing that, you can see the result in half a minute! 270 1 1 1 Part A Question 6 – Speeding Up The Program (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 6 - Answer After the program has finished, what is the letter that you see in the turtle window? You only need to write one capital letter for your answer. import turtle turtle.left(90) for d in range(2): for i in range(2700): turtle.forward(0.1) if d: turtle.right(0.1) else: turtle.left(0.1) turtle.done() The result looks like the letter S (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 7 A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random turtle.speed(1) while True: x = random.randint(0, 500) y = random.randint(0, 500) while x + y > 500: x = random.randint(0, 500) y = random.randint(0, 500) turtle.goto(x - 250, y - 250) turtle.done() (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 7 - Answer A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random turtle.speed(1) while True: x = random.randint(0, 500) y = random.randint(0, 500) while x + y > 500: x = random.randint(0, 500) y = random.randint(0, 500) turtle.goto(x - 250, y - 250) turtle.done() triangle Lots of lines will be drawn, after a while you will see a triangle shape (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random turtle.speed(1) while True: x = random.randint(0, 500) y = random.randint(0, 500) while x + y > 500: x = random.randint(0, 500) y = random.randint(0, 500) turtle.goto(x - 250, y - 250) turtle.done() How to answer this question? If you run the code it is slow, but not terribly slow - if you wait 1.5 minutes you see this: So you could just wait a couple of minutes (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random turtle.speed(1) while True: x = random.randint(0, 500) y = random.randint(0, 500) while x + y > 500: x = random.randint(0, 500) y = random.randint(0, 500) turtle.goto(x - 250, y - 250) turtle.done() To speed things up, you could change turtle.speed(1) to turtle.speed(0) Then after a few seconds you can see the shape: (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part A Question 7 - Discussion A Python program has been written with an infinite loop (a loop that never ends). If you run the program for a long time, what is the shape that you will see in the turtle window? Write one single English word to describe the shape. import turtle import random turtle.speed(1) while True: x = random.randint(0, 500) y = random.randint(0, 500) while x + y > 500: x = random.randint(0, 500) y = random.randint(0, 500) turtle.goto(x - 250, y - 250) turtle.done() Another way to get the answer is to look at the code Consequences: If y is big, then x cannot be big If x is big, then y cannot be big This code means x + y is fixed to a maximum (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. COMP1021 Introduction to Computer Science Fall 2020 Midterm Exam Part B Questions (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. import turtle turtle.forward(1) turtle.forward(1) turtle.forward(1) turtle.forward(1) turtle.forward(1) step=6 while step!=11: print("in outer loop") turtle.forward(5) turtle.forward(5) change=1 while change>3: print("in inner loop") turtle.forward(10) change=change+10 step=step+1 Part B Question 1 What is the x position of the turtle after the program has finished? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 1 - Answer What is the x position of the turtle after the program has finished? import turtle turtle.forward(1) turtle.forward(1) turtle.forward(1) turtle.forward(1) turtle.forward(1) step=6 while step!=11: print("in outer loop") turtle.forward(5) turtle.forward(5) change=1 while change>3: print("in inner loop") turtle.forward(10) change=change+10 step=step+1 Move forward 5 The outer loop runs 5 times Each time the turtle moves forward 10 The inner loop runs 0 times, so you can ignore it 5 + (10 * 5) = 55 The final x position is: (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 2 Week 1 Lecture 1 Lecture 2 Week 2 Lecture 1 Lecture 2 Week 3 Lecture 1 Lecture 2 Week 4 Lecture 1 Week 5 Lecture 1 Lecture 2 Week 6 Lecture 1 Lecture 2 Midterm Practice Exam Week 7 Lecture 1 Lecture 2 Actual Midterm Exam Week 8 Lecture 2 Week 9 Lecture 1 Lecture 2 Week 10 Lecture 1 Lecture 2 Week 11 Lecture 1 Lecture 2 Week 12 Lecture 1 Lecture 2 Week 13 Lecture 1 Lecture 2 Week 14 Exam week (half) - Final Exam date unknown Week 15 Exam week - Final Exam date unknown Week 16 Exam week (half) - Final Exam date unknown for week in range(1, 17) : print("Week", week, end=" ") events="" if week>=14 : if week%2==0 : events=events + "Exam week (half) " else: events=events + "Exam week " events=events + "- Final Exam date unknown " else: if week!=8: events=events + "Lecture 1 " if week!=4: events=events + "Lecture 2 " if week==6: events=events + "Midterm Practice Exam " if week==7: events=events + "Actual Midterm Exam " print(events) In the appropriate places, select the three missing pieces of code. Here is the result of the program: Blank1 – Select one: range(1, 18) range(1, 17) range(1, 16) range(0, 17) range(0, 16) range(0, 15) Blank2 – Select one: week>=15 week>=14 week>=13 week<=15 week<=14 week<=13 Blank3 – Select one: week%2==0 week%2=0 week/2==0 week/2=0 week%2==1 week%2=1 week/2=1 blank1 blank2 blank3 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 2 - Answer Week 1 Lecture 1 Lecture 2 Week 2 Lecture 1 Lecture 2 Week 3 Lecture 1 Lecture 2 Week 4 Lecture 1 Week 5 Lecture 1 Lecture 2 Week 6 Lecture 1 Lecture 2 Midterm Practice Exam Week 7 Lecture 1 Lecture 2 Actual Midterm Exam Week 8 Lecture 2 Week 9 Lecture 1 Lecture 2 Week 10 Lecture 1 Lecture 2 Week 11 Lecture 1 Lecture 2 Week 12 Lecture 1 Lecture 2 Week 13 Lecture 1 Lecture 2 Week 14 Exam week (half) - Final Exam date unknown Week 15 Exam week - Final Exam date unknown Week 16 Exam week (half) - Final Exam date unknown for week in range(1, 17) : print("Week", week, end=" ") events="" if week>=14 : if week%2==0 : events=events + "Exam week (half) " else: events=events + "Exam week " events=events + "- Final Exam date unknown " else: if week!=8: events=events + "Lecture 1 " if week!=4: events=events + "Lecture 2 " if week==6: events=events + "Midterm Practice Exam " if week==7: events=events + "Actual Midterm Exam " print(events) In the appropriate places, select the three missing pieces of code. Here is the result of the program: Blank1 – Select one: range(1, 18) range(1, 17) range(1, 16) range(0, 17) range(0, 16) range(0, 15) Blank2 – Select one: week>=15 week>=14 week>=13 week<=15 week<=14 week<=13 Blank3 – Select one: week%2==0 week%2=0 week/2==0 week/2=0 week%2==1 week%2=1 week/2=1 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. import turtle import random turtle.speed(0) turtle.hideturtle() colours = [ "red", "orange", "yellow", "green", "blue", "violet" ] num_colours = len(colours) for i in range(num_colours, 0, -1): turtle.forward(i*30) turtle.left(90) turtle.fillcolor(colours[num_colours-i]) turtle.begin_fill() turtle.circle(i*30, 180) turtle.end_fill() turtle.left(90) turtle.forward(i*30) turtle.done() Part B Question 3 Here is a Python program. The person who made the program wants the program to draw this: Unfortunately, the program shown above does not draw the image shown above. One line of code needs to be edited Which line of code needs to be edited? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. import turtle import random turtle.speed(0) turtle.hideturtle() colours = [ "red", "orange", "yellow", "green", "blue", "violet" ] num_colours = len(colours) for i in range(num_colours, 0, -1): turtle.forward(i*30) turtle.left(90) turtle.fillcolor(colours[num_colours-i]) turtle.begin_fill() turtle.circle(i*30, 180) turtle.end_fill() turtle.left(90) turtle.forward(i*30) turtle.done() Part B Question 3 - Answer Here is a Python program. The person who made the program wants the program to draw this: Unfortunately, the program shown above does not draw the image shown above. One line of code needs to be edited Which line of code needs to be edited? We need to add "black" to the end of the list in line 5 You can see there is a black semi-circle (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. # 012345678901234567890123456789012345 x = "i attended python classes in school!" part1 = x[int(len(x)/3)-1:int(len(x)/2)] part2 = x[0] + x[-7:-9:-1] part3 = x[-6] + x[-4:-1] result = part1+part2+part3 print(result) Part B Question 4 When the following program is run, what is printed? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. # 012345678901234567890123456789012345 x = "i attended python classes in school!" part1 = x[int(len(x)/3)-1:int(len(x)/2)] part2 = x[0] + x[-7:-9:-1] part3 = x[-6] + x[-4:-1] result = part1+part2+part3 print(result) Part B Question 4 - Answer When the following program is run, what is printed? "python " "i" + "s " "c" + "ool" The answer is: python is cool a space is here a space is here (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 5 Two students form a team to answer a set of questions in a competition. They get points by answering questions. No error occurs when the program is run. According to the last message shown by the program, what are the total points they get in the competition? Enter one integer. (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. number_of_questions = 30 total_points = 0 next_student_to_answer = "student1” for question in range(number_of_questions): if question >= 15: break elif question % 2: continue elif question == 10: print("Both students answer question",\ question, "together") total_points += 50 elif next_student_to_answer == "student1": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 10 next_student_to_answer = "student2" elif next_student_to_answer == "student2": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 20 next_student_to_answer = "student1" print("The total points they get in the \ competition is", total_points) What are the total points they get in the competition? Part B Question 5 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. number_of_questions = 30 total_points = 0 next_student_to_answer = "student1” for question in range(number_of_questions): if question >= 15: break elif question % 2: continue elif question == 10: print("Both students answer question",\ question, "together") total_points += 50 elif next_student_to_answer == "student1": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 10 next_student_to_answer = "student2" elif next_student_to_answer == "student2": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 20 next_student_to_answer = "student1" print("The total points they get in the \ competition is", total_points) What are the total points they get in the competition? Part B Question 5 - Discussion What about this code? Remember you can always use th shell to quickly try thi gs out: You can see that total_points += 50 is the same as total_points = total_points + 50 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. number_of_questions = 30 total_points = 0 next_student_to_answer = "student1” for question in range(number_of_questions): if question >= 15: break elif question % 2: continue elif question == 10: print("Both students answer question",\ question, "together") total_points += 50 elif next_student_to_answer == "student1": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 10 next_student_to_answer = "student2" elif next_student_to_answer == "student2": print(next_student_to_answer, "answers \ question", question) total_points = total_points + 20 next_student_to_answer = "student1" print("The total points they get in the \ competition is", total_points) odd questions will be skipped question >= 15 will be skipped Question 10: 50 points Student 1 answered question 0, 4, 8, 14; 10 * 4 = 40 points Student 2 answered question 2, 6, 12; 20 * 3 = 60 points Part B Question 5 - Discussion (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 5 - Answer The answer is 150 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 6 A program has been written to count some fruits that you receive from a farm. Here is an example of using the program. The user input is shown in bold. Give me apple, coconut, cherry, grape, lychee, orange, peach or pear. Enter none when you finish. What is the fruit? grape What is the fruit? peach What is the fruit? apple What is the fruit? orange What is the fruit? peach What is the fruit? orange What is the fruit? peach What is the fruit? peach What is the fruit? pear What is the fruit? coconut What is the fruit? none Thank you! Here is what I have got: apple 1 coconut 1 cherry 0 grape 1 lychee 0 orange 2 peach 4 pear 1 (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. myfruit_names = ["apple", "coconut", "cherry", "grape", \ "lychee", "orange", "peach", "pear"] myfruit_numbers = [] for _ in myfruit_names: myfruit_numbers.append(0) print("Give me apple, coconut, cherry, grape,", end=" ") print("lychee, orange, peach or pear.") print("Enter none when you finish.") received_fruits = [] fruit = "" while fruit != "none": fruit = input("What is the fruit? ") received_fruits.append(fruit) for i in range(len(myfruit_names)): = received_fruits.count( ) print("Thank you!") print() ? Part B Question 6 print("Here is what I have got:") for i in range(len(myfruit_names)): print(myfruit_names[i], myfruit_numbers[i]) ? (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. Part B Question 6 - Answer myfruit_names = ["apple", "coconut", "cherry", "grape", \ "lychee", "orange", "peach", "pear"] myfruit_numbers = [] for _ in myfruit_names: myfruit_numbers.append(0) print("Give me apple, coconut, cherry, grape,", end=" ") print("lychee, orange, peach or pear.") print("Enter none when you finish.") received_fruits = [] fruit = "" while fruit != "none": fruit = input("What is the fruit? ") received_fruits.append(fruit) for i in range(len(myfruit_names)): myfruit_numbers[i] = received_fruits.count( myfruit_names[i] ) print("Thank you!") print() print("Here is what I have got:") for i in range(len(myfruit_names)): print(myfruit_names[i], myfruit_numbers[i]) (COMP1021)[2020](f)midterm~=db7k_5m^_84597.pdf downloaded by xzhangfr from http://petergao.net/ustpastpaper/down.php?course=COMP1021&id=17 at 2021-10-06 02:11:39. Academic use within HKUST only. 





























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































essay、essay代写