labNo
float64
1
10
taskNo
float64
0
4
questioner
stringclasses
2 values
question
stringlengths
9
201
code
stringlengths
18
30.3k
startLine
float64
0
192
endLine
float64
0
196
questionType
stringclasses
4 values
answer
stringlengths
2
905
src
stringclasses
3 values
code_processed
stringlengths
12
28.3k
id
stringlengths
2
5
raw_code
stringlengths
20
30.3k
raw_comment
stringlengths
10
242
comment
stringlengths
9
207
q_code
stringlengths
66
30.3k
3
2
TA
Why is it only turning right from line 45 until i<9?
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo = Robot() hubo.set_trace('blue') hubo.set_pause(0.1) turn_check = 1 def turn_right(): for i in range(3): hubo.turn_left() def go_straight(): while hubo.front_is_clear(): hubo.move() def pick_and_move(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() def turn_left_left(): hubo.turn_left() pick_and_move() hubo.turn_left() def turn_right_right(): turn_right() pick_and_move() turn_right() def drop_all(): while hubo.carries_beepers(): hubo.drop_beeper() for i in range(10): while hubo.front_is_clear(): pick_and_move() if turn_check: turn_left_left() turn_check = 0 elif i<9: turn_right_right() turn_check = 1 while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() go_straight() drop_all() hubo.turn_left()
38
46
reasoning
If i <9, it was bent to the right at 10, 10 and an error occurred, so I handled it separately.
cs1qa
null
null
null
null
null
Question: Why is it only turning right from line 45 until i<9? Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo = Robot() hubo.set_trace('blue') hubo.set_pause(0.1) turn_check = 1 def turn_right(): for i in range(3): hubo.turn_left() def go_straight(): while hubo.front_is_clear(): hubo.move() def pick_and_move(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() def turn_left_left(): hubo.turn_left() pick_and_move() hubo.turn_left() def turn_right_right(): turn_right() pick_and_move() turn_right() def drop_all(): while hubo.carries_beepers(): hubo.drop_beeper() for i in range(10): while hubo.front_is_clear(): pick_and_move() if turn_check: turn_left_left() turn_check = 0 elif i<9: turn_right_right() turn_check = 1 while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() go_straight() drop_all() hubo.turn_left()
4
1
TA
Why should we apply float() after receiving input() in Task 2?
def is_triangle(a, b, c): if ((a+b>c) and (b+c>a) and (c+a>b)): return True else: return False a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) if is_triangle(a, b, c): print('YES') else: print('NO')
7
9
reasoning
This is because the input() function receives input in the form of a string.
cs1qa
null
null
null
null
null
Question: Why should we apply float() after receiving input() in Task 2? Code: def is_triangle(a, b, c): if ((a+b>c) and (b+c>a) and (c+a>b)): return True else: return False a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) if is_triangle(a, b, c): print('YES') else: print('NO')
1
0
TA
Briefly explain f and m among the functions you have implemented
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def f(): for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() def m(): hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(4): f() m() f()
5
21
variable
f() is a function that rearranges the direction so that it goes up and down as if it is turned over, and m() is a function that moves it one space to the right and re-orients it up again.
cs1qa
null
null
null
null
null
Question: Briefly explain f and m among the functions you have implemented Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def f(): for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() def m(): hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(4): f() m() f()
3
1
TA
Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper.
from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash2.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() hubo.set_trace('red') def turn_around(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn_around() while hubo.front_is_clear(): hubo.move() for i in range(3): hubo.turn_left() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn_around() hubo.move() hubo.turn_left()
12
13
reasoning
If the for statement is used, when the number of beepers changes, it cannot be retrieved accordingly, so I used the while statement.
cs1qa
null
null
null
null
null
Question: Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper. Code: from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash2.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() hubo.set_trace('red') def turn_around(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn_around() while hubo.front_is_clear(): hubo.move() for i in range(3): hubo.turn_left() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn_around() hubo.move() hubo.turn_left()
6
0
TA
What is the role of f_temp in task1?
def fibonacci(upper_bound): f1=0 f2=1 fibonacci=[f1,f2] f_temp=0 while 1: f_temp=f1 f_temp+=f2 if f_temp>upper_bound: break else: fibonacci.append(f_temp) f1=f2 f2=f_temp return fibonacci print(fibonacci(1000))
4
13
variable
After finding f_n, you need both f_n and f_(n-1) to find f_(n+1). At this time, f_n is stored for a while!
cs1qa
null
null
null
null
null
Question: What is the role of f_temp in task1? Code: def fibonacci(upper_bound): f1=0 f2=1 fibonacci=[f1,f2] f_temp=0 while 1: f_temp=f1 f_temp+=f2 if f_temp>upper_bound: break else: fibonacci.append(f_temp) f1=f2 f2=f_temp return fibonacci print(fibonacci(1000))
1
4
TA
Please explain #5
from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest_upside(): turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_downside(): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() turn_right for i in range(5): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_twoline(): for i in range(5): harvest_upside() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() harvest_downside() for i in range(4): turn_right() harvest_downside() for i in range(2): harvest_twoline() turn_right() hubo.move() turn_right() hubo. move() hubo.pick_beeper() harvest_twoline()
23
48
code_explain
First, go to the 6th and 2nd points After harvesting, going one space to the left and one space to the right After harvesting all the rows At points 11 and 7 Go to point 11, 8 After going to the 10th and 8th points Also, repeating the left and the bottom, we harvested the second row. I repeated it 3 times.
cs1qa
null
null
null
null
null
Question: Please explain #5 Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest_upside(): turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_downside(): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() turn_right for i in range(5): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_twoline(): for i in range(5): harvest_upside() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() harvest_downside() for i in range(4): turn_right() harvest_downside() for i in range(2): harvest_twoline() turn_right() hubo.move() turn_right() hubo. move() hubo.pick_beeper() harvest_twoline()
6
2
student
Can you explain what the print_cards() function is about in 3?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i%2 == 1: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if num_pads[num1]==num_pads[num2]: tries +=1 return True ########################################################################### else: tries +=1 return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs tries=str(tries) if tries.endswith("1"): if tries==11: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries.endswith("2"): if tries==12: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif tries.endswith("3"): if tries==13: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
38
59
code_understanding
It is a function that prints cards on the screen based on the current information.
cs1qa
null
null
null
null
null
Question: Can you explain what the print_cards() function is about in 3? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i%2 == 1: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if num_pads[num1]==num_pads[num2]: tries +=1 return True ########################################################################### else: tries +=1 return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs tries=str(tries) if tries.endswith("1"): if tries==11: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries.endswith("2"): if tries==12: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif tries.endswith("3"): if tries==13: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
1
3
TA
Can you explain pick_beeper_oneline() and next_line_left_pick_beepers_oneline() in task4?
from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def pick_beeper_move(): hubo.pick_beeper() hubo.move() def pick_beeper_oneline(): for i in range(5): pick_beeper_move() hubo.pick_beeper() def next_line_left_pick_beepers_oneline(): hubo.turn_left() hubo.move() hubo.turn_left() pick_beeper_oneline() def next_line_right_pick_beepers_oneline(): turn_right() hubo.move() turn_right() pick_beeper_oneline() hubo.move() pick_beeper_oneline() next_line_left_pick_beepers_oneline() next_line_right_pick_beepers_oneline() next_line_left_pick_beepers_oneline() next_line_right_pick_beepers_oneline() next_line_left_pick_beepers_oneline()
15
24
variable
pick_beeper_oneline() picks up all the beepers horizontally, and next_line_left_pick_beepers_oneline() turns left and goes up to the next line.
cs1qa
null
null
null
null
null
Question: Can you explain pick_beeper_oneline() and next_line_left_pick_beepers_oneline() in task4? Code: from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def pick_beeper_move(): hubo.pick_beeper() hubo.move() def pick_beeper_oneline(): for i in range(5): pick_beeper_move() hubo.pick_beeper() def next_line_left_pick_beepers_oneline(): hubo.turn_left() hubo.move() hubo.turn_left() pick_beeper_oneline() def next_line_right_pick_beepers_oneline(): turn_right() hubo.move() turn_right() pick_beeper_oneline() hubo.move() pick_beeper_oneline() next_line_left_pick_beepers_oneline() next_line_right_pick_beepers_oneline() next_line_left_pick_beepers_oneline() next_line_right_pick_beepers_oneline() next_line_left_pick_beepers_oneline()
1
0
TA
What is the zigzag function in task 1?
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def zigzag(): hubo.turn_left() for i in range(9): hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() for i in range(9): hubo.move() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
6
16
variable
It is a function that goes back and forth once from top to bottom.
cs1qa
null
null
null
null
null
Question: What is the zigzag function in task 1? Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def zigzag(): hubo.turn_left() for i in range(9): hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() for i in range(9): hubo.move() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
3
0
TA
What's the reason you rotate the gun 4 times in the first task?
from cs1robots import * create_world() hubo = Robot(orientation = 'W', avenue=7, street=5) for i in range(4): if not hubo.facing_north(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
4
6
reasoning
In order to get north in four turns. I think it's okay to change it up to three times
cs1qa
null
null
null
null
null
Question: What's the reason you rotate the gun 4 times in the first task? Code: from cs1robots import * create_world() hubo = Robot(orientation = 'W', avenue=7, street=5) for i in range(4): if not hubo.facing_north(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
6
2
TA
What is the case for num1 and num2 that return False when is_valid in Task 3?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) rect.setFillColor("white") text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list ################################################################ random.shuffle(cards) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ count = correct_list.count(i) if (count==0) : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### num1yes = correct_list.count(num1) num2yes = correct_list.count(num2) if (num1yes!=0 or num2yes!=0): return False elif (num1>24 or num2>24 or num1<0 or num2<0): return False elif (num1==num2): return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.remove(num_pads[i]) elif i==num2: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.remove(num_pads[i]) #break w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if (cards[num1][1] == cards[num2][1]): correct_list.append(num1) correct_list.append(num2) return True else: w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if (i==num1 or i ==num2) : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 return False #-------------------------------------------------------------------------------------- initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)!=24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries+1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
81
99
variable
If it already exists in correct_list, if num1 and num2 are the same, returns False if it is not an integer from 0 to 23.
cs1qa
null
null
null
null
null
Question: What is the case for num1 and num2 that return False when is_valid in Task 3? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) rect.setFillColor("white") text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list ################################################################ random.shuffle(cards) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ count = correct_list.count(i) if (count==0) : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### num1yes = correct_list.count(num1) num2yes = correct_list.count(num2) if (num1yes!=0 or num2yes!=0): return False elif (num1>24 or num2>24 or num1<0 or num2<0): return False elif (num1==num2): return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.remove(num_pads[i]) elif i==num2: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.remove(num_pads[i]) #break w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if (cards[num1][1] == cards[num2][1]): correct_list.append(num1) correct_list.append(num2) return True else: w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if (i==num1 or i ==num2) : # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 return False #-------------------------------------------------------------------------------------- initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)!=24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries+1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
9
1
TA
Please briefly explain how your 5 functions work!
import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Cards : def setdata1(self,suit,face,Img,value) : self.suit = suit self.face = face self.img = Img self.value = value def setdata2(self, hid_or_not) : self.state = hid_or_not def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ cards = [] for i in range (4) : for k in range (13) : img_code = Image(img_path+suit_names[i]+'_'+face_names[k]+'.png') C = Cards() C.setdata1(suit_names[i],face_names[i], img_code, value[k]) C.setdata2(True) cards.append(C) random.shuffle(cards) return cards def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ give_money = 0 for i in range(len(hand)) : give_money = give_money + hand[i].value return give_money def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ card_name_nicely = str('a ' + card.face + ' of ' + card.suit) return card_name_nicely def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ while True : ask = input(prompt) if ask == 'y': return True elif ask == 'n': return False else : print("I beg your pardon!") continue def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in range(len(dealer)): if dealer[i].state: bj_board.add(dealer[i].img) dealer[i].img.moveTo(x0+i*20,y0) dealer[i].img.setDepth(depth-10*i) else: back_of_the_card_image=Image(img_path+'Back.png') bj_board.add(back_of_the_card_image) back_of_the_card_image.moveTo(x0+i*20,y0) back_of_the_card_image.setDepth(depth-10*i) for i in range(len(player)): if player[i].state: bj_board.add(player[i].img) player[i].img.moveTo(x1+i*20,y1) player[i].img.setDepth(depth-10*i) else: back_of_the_card_image=Image(img_path+'Back.png') bj_board.add(back_of_the_card_image) back_of_the_card_image.moveTo(x1+i*20,y1) back_of_the_card_image.setDepth(depth-10*i) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
28
44
variable
The create_deck function is a function that shuffles after entering information about the card information (pattern, number, value) for each 52 cards.
cs1qa
null
null
null
null
null
Question: Please briefly explain how your 5 functions work! Code: import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Cards : def setdata1(self,suit,face,Img,value) : self.suit = suit self.face = face self.img = Img self.value = value def setdata2(self, hid_or_not) : self.state = hid_or_not def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ cards = [] for i in range (4) : for k in range (13) : img_code = Image(img_path+suit_names[i]+'_'+face_names[k]+'.png') C = Cards() C.setdata1(suit_names[i],face_names[i], img_code, value[k]) C.setdata2(True) cards.append(C) random.shuffle(cards) return cards def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ give_money = 0 for i in range(len(hand)) : give_money = give_money + hand[i].value return give_money def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ card_name_nicely = str('a ' + card.face + ' of ' + card.suit) return card_name_nicely def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ while True : ask = input(prompt) if ask == 'y': return True elif ask == 'n': return False else : print("I beg your pardon!") continue def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in range(len(dealer)): if dealer[i].state: bj_board.add(dealer[i].img) dealer[i].img.moveTo(x0+i*20,y0) dealer[i].img.setDepth(depth-10*i) else: back_of_the_card_image=Image(img_path+'Back.png') bj_board.add(back_of_the_card_image) back_of_the_card_image.moveTo(x0+i*20,y0) back_of_the_card_image.setDepth(depth-10*i) for i in range(len(player)): if player[i].state: bj_board.add(player[i].img) player[i].img.moveTo(x1+i*20,y1) player[i].img.setDepth(depth-10*i) else: back_of_the_card_image=Image(img_path+'Back.png') bj_board.add(back_of_the_card_image) back_of_the_card_image.moveTo(x1+i*20,y1) back_of_the_card_image.setDepth(depth-10*i) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
2
2
TA
How can I write a condition in task3 other than ==False?
from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') # load_world('worlds/hurdles2.wld') load_world('worlds/hurdles3.wld') hubo = Robot() hubo.set_trace('Blue') def turn_right(): for i in range(3): hubo.turn_left() def jump_one_hurdle(): if not hubo.front_is_clear(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() else: hubo.move() while hubo.on_beeper()== False : jump_one_hurdle()
29
29
code_explain
at least with while not hubo.on_beeper().
cs1qa
null
null
null
null
null
Question: How can I write a condition in task3 other than ==False? Code: from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') # load_world('worlds/hurdles2.wld') load_world('worlds/hurdles3.wld') hubo = Robot() hubo.set_trace('Blue') def turn_right(): for i in range(3): hubo.turn_left() def jump_one_hurdle(): if not hubo.front_is_clear(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() else: hubo.move() while hubo.on_beeper()== False : jump_one_hurdle()
2
4
TA
Why does the while statement exit when there is a wall on the left and right?
from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=4, streets=9) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... my_robot=Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() turn=1 my_robot.turn_left() while True: while my_robot.front_is_clear(): my_robot.move() if turn: if my_robot.right_is_clear()==False: break else: turn_right() my_robot.move() turn_right() turn=False else: if my_robot.left_is_clear()==False: break else: my_robot.turn_left() my_robot.move() my_robot.turn_left() turn=True
22
27
reasoning
I used break to set the robot to stop at the last avenue.
cs1qa
null
null
null
null
null
Question: Why does the while statement exit when there is a wall on the left and right? Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=4, streets=9) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... my_robot=Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() turn=1 my_robot.turn_left() while True: while my_robot.front_is_clear(): my_robot.move() if turn: if my_robot.right_is_clear()==False: break else: turn_right() my_robot.move() turn_right() turn=False else: if my_robot.left_is_clear()==False: break else: my_robot.turn_left() my_robot.move() my_robot.turn_left() turn=True
5
0
TA
Why use global balance in task1?
balance = 0 def deposit(money) : global balance balance = balance + money return balance def withdrawal(money) : global balance balance = balance - money return balance def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)??") if(process=="d"): c=int(input("How much do you want to deposit?")) deposit(c) print("you deposited %s won"% str(c)) elif(process=="w"): c=int(input("How much do you want to withdraw?")) if(c>balance): print("you've withdrawn %s won"% str(c)) print("But you only have %d won"% balance) else: withdrawal(c) print("you've withdraw %s won"% str(c)) elif(process=="c"): print("your current balance is %d won"% balance) elif(process==""): return else: print("please press d or w or c or return") bank()
0
39
reasoning
It is okay to use a local variable, but before solving the problem, balance was already declared at the top, so I used it as a global variable.
cs1qa
null
null
null
null
null
Question: Why use global balance in task1? Code: balance = 0 def deposit(money) : global balance balance = balance + money return balance def withdrawal(money) : global balance balance = balance - money return balance def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)??") if(process=="d"): c=int(input("How much do you want to deposit?")) deposit(c) print("you deposited %s won"% str(c)) elif(process=="w"): c=int(input("How much do you want to withdraw?")) if(c>balance): print("you've withdrawn %s won"% str(c)) print("But you only have %d won"% balance) else: withdrawal(c) print("you've withdraw %s won"% str(c)) elif(process=="c"): print("your current balance is %d won"% balance) elif(process==""): return else: print("please press d or w or c or return") bank()
4
2
TA
Please explain task3
import math sin = math.sin pi = math.pi n=input('How many steps? ') n=int(n) for i in range(n): x = float(i) / (n-1) * 2 * pi print (sin(x))
2
8
code_explain
task3 changed the type of the value received as an input to int so that n can be used in the for statement.
cs1qa
null
null
null
null
null
Question: Please explain task3 Code: import math sin = math.sin pi = math.pi n=input('How many steps? ') n=int(n) for i in range(n): x = float(i) / (n-1) * 2 * pi print (sin(x))
5
1
TA
Please explain next task2
from cs1graphics import * from time import sleep from math import * def draw_animal(): canvas=Canvas(400,300) canvas.setBackgroundColor('light blue') canvas.setTitle('fish') rc1=Rectangle(20,5,Point(-7,2)) rc1.setBorderColor('red') rc1.setFillColor('red') rc2=Rectangle(20,5,Point(-7,-2)) rc2.setBorderColor('red') rc2.setFillColor('red') ci=Circle(6,Point(1,0)) ci.setBorderColor('red') ci.setFillColor('red') rc2.rotate(25) rc1.rotate(-25) tail=Layer() tail.add(rc1) tail.add(rc2) fish=Layer() fish.add(ci) fish.add(tail) tail.setDepth(20) ci.setDepth(10) fish.moveTo(40,150) fish.scale(2) canvas.add(fish) def show_animation(): canvas=Canvas(400,300) canvas.setBackgroundColor('light blue') canvas.setTitle('fish') rc1=Rectangle(20,5,Point(-7,2)) rc1.setBorderColor('red') rc1.setFillColor('red') rc2=Rectangle(20,5,Point(-7,-2)) rc2.setBorderColor('red') rc2.setFillColor('red') ci=Circle(6,Point(1,0)) ci.setBorderColor('red') ci.setFillColor('red') rc2.rotate(25) rc1.rotate(-25) tail=Layer() tail.add(rc1) tail.add(rc2) fish=Layer() fish.add(ci) fish.add(tail) tail.setDepth(20) ci.setDepth(10) fish.moveTo(40,150) fish.scale(2) canvas.add(fish) for i in range(17): for i in range(10): tail.rotate(1) fish.move(1,0) sleep(0.1) for i in range(10): tail.rotate(-1) fish.move(1,0) sleep(0.1) draw_animal() show_animation()
4
66
code_explain
I made a picture of a fish and an animation where the fish swims forward.
cs1qa
null
null
null
null
null
Question: Please explain next task2 Code: from cs1graphics import * from time import sleep from math import * def draw_animal(): canvas=Canvas(400,300) canvas.setBackgroundColor('light blue') canvas.setTitle('fish') rc1=Rectangle(20,5,Point(-7,2)) rc1.setBorderColor('red') rc1.setFillColor('red') rc2=Rectangle(20,5,Point(-7,-2)) rc2.setBorderColor('red') rc2.setFillColor('red') ci=Circle(6,Point(1,0)) ci.setBorderColor('red') ci.setFillColor('red') rc2.rotate(25) rc1.rotate(-25) tail=Layer() tail.add(rc1) tail.add(rc2) fish=Layer() fish.add(ci) fish.add(tail) tail.setDepth(20) ci.setDepth(10) fish.moveTo(40,150) fish.scale(2) canvas.add(fish) def show_animation(): canvas=Canvas(400,300) canvas.setBackgroundColor('light blue') canvas.setTitle('fish') rc1=Rectangle(20,5,Point(-7,2)) rc1.setBorderColor('red') rc1.setFillColor('red') rc2=Rectangle(20,5,Point(-7,-2)) rc2.setBorderColor('red') rc2.setFillColor('red') ci=Circle(6,Point(1,0)) ci.setBorderColor('red') ci.setFillColor('red') rc2.rotate(25) rc1.rotate(-25) tail=Layer() tail.add(rc1) tail.add(rc2) fish=Layer() fish.add(ci) fish.add(tail) tail.setDepth(20) ci.setDepth(10) fish.moveTo(40,150) fish.scale(2) canvas.add(fish) for i in range(17): for i in range(10): tail.rotate(1) fish.move(1,0) sleep(0.1) for i in range(10): tail.rotate(-1) fish.move(1,0) sleep(0.1) draw_animal() show_animation()
3
1
TA
Is there any special reason for writing the while statement in Task2 line13?
from cs1robots import * # Your code must work for all world files below. #load_world( "worlds/trash1.wld" ) load_world( "worlds/trash2.wld" ) my_robot = Robot() def turn_right(): for i in range(3): my_robot.turn_left() number_beeper=0 while my_robot.carries_beepers: while my_robot.on_beeper(): my_robot.pick_beeper() number_beeper=number_beeper+1 if not my_robot.front_is_clear(): break my_robot.move() my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_right() my_robot.move() for i in range(number_beeper): my_robot.drop_beeper()
12
18
reasoning
I used it to keep the inner loops repeatable while the robot moves the beeper.
cs1qa
null
null
null
null
null
Question: Is there any special reason for writing the while statement in Task2 line13? Code: from cs1robots import * # Your code must work for all world files below. #load_world( "worlds/trash1.wld" ) load_world( "worlds/trash2.wld" ) my_robot = Robot() def turn_right(): for i in range(3): my_robot.turn_left() number_beeper=0 while my_robot.carries_beepers: while my_robot.on_beeper(): my_robot.pick_beeper() number_beeper=number_beeper+1 if not my_robot.front_is_clear(): break my_robot.move() my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_right() my_robot.move() for i in range(number_beeper): my_robot.drop_beeper()
2
0
TA
What is harvest() function in task1?
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def curve(): for i in range(5): harvest() hubo.turn_left() harvest() hubo.turn_left() for i in range(5): harvest() turn_right() harvest() for i in range(2): curve() harvest() turn_right() curve()
7
10
variable
Moving After checking if there is a beeper Is a pick function
cs1qa
null
null
null
null
null
Question: What is harvest() function in task1? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def harvest(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def curve(): for i in range(5): harvest() hubo.turn_left() harvest() hubo.turn_left() for i in range(5): harvest() turn_right() harvest() for i in range(2): curve() harvest() turn_right() curve()
8
2
TA
%6.1f How would a number come out by writing like this?
import elice_utils Month = [[],[],[],[],[],[],[],[],[],[],[],[]] def listing(): data = open("tpmon.txt", "r") first = data.readline() for line in data: s = line.split(" ") while "" in s: s.remove("") for i in range(len(s)-1): Month[i].append(s[i]) data.close() def average(): winavg = [] sumavg = [] for i in range(len(Month[0])): winavg.append((float(Month[0][i])+float(Month[1][i]))/2) sumavg.append((float(Month[6][i])+float(Month[7][i]))/2) print('%d : %6.1f / %4.1f' % (i+1723 , winavg[i], sumavg[i])) def output(): data = open("tpmon.csv", "w") for i in range(len(Month[0])): string = "" year = i + 1723 string += str(year) string += "," for k in range(12): string += Month[k][i] string += "," data.write(string + "\n") data.close() elice_utils.send_file('tpmon.csv') listing() average() output()
20
20
variable
It seems to print to the first decimal place.
cs1qa
null
null
null
null
null
Question: %6.1f How would a number come out by writing like this? Code: import elice_utils Month = [[],[],[],[],[],[],[],[],[],[],[],[]] def listing(): data = open("tpmon.txt", "r") first = data.readline() for line in data: s = line.split(" ") while "" in s: s.remove("") for i in range(len(s)-1): Month[i].append(s[i]) data.close() def average(): winavg = [] sumavg = [] for i in range(len(Month[0])): winavg.append((float(Month[0][i])+float(Month[1][i]))/2) sumavg.append((float(Month[6][i])+float(Month[7][i]))/2) print('%d : %6.1f / %4.1f' % (i+1723 , winavg[i], sumavg[i])) def output(): data = open("tpmon.csv", "w") for i in range(len(Month[0])): string = "" year = i + 1723 string += str(year) string += "," for k in range(12): string += Month[k][i] string += "," data.write(string + "\n") data.close() elice_utils.send_file('tpmon.csv') listing() average() output()
10
0
TA
Please explain briefly one by one what the attributes of the created Car class are.
from cs1graphics import * from time import sleep canvas = Canvas (900, 600) canvas.setBackgroundColor("light blue") class Car(object): def __init__(self, bodycolor='Blue', wheelcolor='Grey',state = 'slow' ): self.bodycolor = bodycolor self.wheelcolor = wheelcolor self.state = state car = Layer() wheel1 = Layer() wheel2 = Layer() tire1 = Circle(20, Point(-40,-10)) tire1.setFillColor('black') wheel1a = Rectangle(30,5, Point(0,0)) wheel1a.setFillColor(wheelcolor) wheel1b = Rectangle(5,30, Point(0,0)) wheel1b.setFillColor(wheelcolor) car.add(tire1) wheel1.add(wheel1a) wheel1.add(wheel1b) tire2 = Circle(20, Point(40,-10)) tire2.setFillColor('black') wheel2a = Rectangle(30,5, Point(0,0)) wheel2a.setFillColor(wheelcolor) wheel2b = Rectangle(5,30, Point(0,0)) wheel2b.setFillColor(wheelcolor) car.add(tire2) wheel2.add(wheel2a) wheel2.add(wheel2b) body = Rectangle(140, 60, Point(0,-50)) body.setFillColor(bodycolor) body.setDepth(60) car.add(body) canvas.add(car) car.moveTo(200,200) canvas.add(wheel1) wheel1.moveTo(160,190) canvas.add(wheel2) wheel2.moveTo(240,190) self.car = car self.body = body self.wheel1 = wheel1 self.wheel2 = wheel2 def move(self, x, y): if self.state == 'fast': smoke1 = Rectangle(30, 10, Point(-85,-30)) smoke1.setFillColor('grey') smoke1.setDepth(50) self.car.add(smoke1) smoke2 = Rectangle(20, 10, Point(-120,-30)) smoke2.setFillColor('grey') smoke2.setDepth(50) self.car.add(smoke2) self.body.setFillColor('dark blue') self.car.move(x, y) self.wheel1.move(x, y) self.wheel2.move(x, y) def rotate(self,x): self.wheel1.rotate(x) self.wheel2.rotate(x) # def draw_animal(): # # Implement this function. # global car # global wheel1 # global wheel2 # tire1 = Circle(20, Point(-40,-10)) # tire1.setFillColor('black') # wheel1a = Rectangle(30,5, Point(0,0)) # wheel1a.setFillColor("grey") # wheel1b = Rectangle(5,30, Point(0,0)) # wheel1b.setFillColor("grey") # car.add(tire1) # wheel1.add(wheel1a) # wheel1.add(wheel1b) # tire2 = Circle(20, Point(40,-10)) # tire2.setFillColor('black') # wheel2a = Rectangle(30,5, Point(0,0)) # wheel2a.setFillColor("grey") # wheel2b = Rectangle(5,30, Point(0,0)) # wheel2b.setFillColor("grey") # car.add(tire2) # wheel2.add(wheel2a) # wheel2.add(wheel2b) # body = Rectangle(140, 60, Point(0,-50)) # body.setFillColor("blue") # body.setDepth(60) # car.add(body) # canvas.add(car) # car.moveTo(200,200) # canvas.add(wheel1) # wheel1.moveTo(160,190) # canvas.add(wheel2) # wheel2.moveTo(240,190) # pass def show_animation(): # Implement this function. global car global wheel for i in range(100): car.move(2,0) car.rotate(5) sleep(0.01) sleep(0.5) car.state="fast" print(car.state) for i in range(200): car.move(7,0) car.rotate(10) sleep(0.01) pass car = Car() show_animation()
48
51
variable
The remaining attributes are car, body, wheel1, and wheel2, which represent the whole car, the body of the car, and two squashes respectively.
cs1qa
null
null
null
null
null
Question: Please explain briefly one by one what the attributes of the created Car class are. Code: from cs1graphics import * from time import sleep canvas = Canvas (900, 600) canvas.setBackgroundColor("light blue") class Car(object): def __init__(self, bodycolor='Blue', wheelcolor='Grey',state = 'slow' ): self.bodycolor = bodycolor self.wheelcolor = wheelcolor self.state = state car = Layer() wheel1 = Layer() wheel2 = Layer() tire1 = Circle(20, Point(-40,-10)) tire1.setFillColor('black') wheel1a = Rectangle(30,5, Point(0,0)) wheel1a.setFillColor(wheelcolor) wheel1b = Rectangle(5,30, Point(0,0)) wheel1b.setFillColor(wheelcolor) car.add(tire1) wheel1.add(wheel1a) wheel1.add(wheel1b) tire2 = Circle(20, Point(40,-10)) tire2.setFillColor('black') wheel2a = Rectangle(30,5, Point(0,0)) wheel2a.setFillColor(wheelcolor) wheel2b = Rectangle(5,30, Point(0,0)) wheel2b.setFillColor(wheelcolor) car.add(tire2) wheel2.add(wheel2a) wheel2.add(wheel2b) body = Rectangle(140, 60, Point(0,-50)) body.setFillColor(bodycolor) body.setDepth(60) car.add(body) canvas.add(car) car.moveTo(200,200) canvas.add(wheel1) wheel1.moveTo(160,190) canvas.add(wheel2) wheel2.moveTo(240,190) self.car = car self.body = body self.wheel1 = wheel1 self.wheel2 = wheel2 def move(self, x, y): if self.state == 'fast': smoke1 = Rectangle(30, 10, Point(-85,-30)) smoke1.setFillColor('grey') smoke1.setDepth(50) self.car.add(smoke1) smoke2 = Rectangle(20, 10, Point(-120,-30)) smoke2.setFillColor('grey') smoke2.setDepth(50) self.car.add(smoke2) self.body.setFillColor('dark blue') self.car.move(x, y) self.wheel1.move(x, y) self.wheel2.move(x, y) def rotate(self,x): self.wheel1.rotate(x) self.wheel2.rotate(x) # def draw_animal(): # # Implement this function. # global car # global wheel1 # global wheel2 # tire1 = Circle(20, Point(-40,-10)) # tire1.setFillColor('black') # wheel1a = Rectangle(30,5, Point(0,0)) # wheel1a.setFillColor("grey") # wheel1b = Rectangle(5,30, Point(0,0)) # wheel1b.setFillColor("grey") # car.add(tire1) # wheel1.add(wheel1a) # wheel1.add(wheel1b) # tire2 = Circle(20, Point(40,-10)) # tire2.setFillColor('black') # wheel2a = Rectangle(30,5, Point(0,0)) # wheel2a.setFillColor("grey") # wheel2b = Rectangle(5,30, Point(0,0)) # wheel2b.setFillColor("grey") # car.add(tire2) # wheel2.add(wheel2a) # wheel2.add(wheel2b) # body = Rectangle(140, 60, Point(0,-50)) # body.setFillColor("blue") # body.setDepth(60) # car.add(body) # canvas.add(car) # car.moveTo(200,200) # canvas.add(wheel1) # wheel1.moveTo(160,190) # canvas.add(wheel2) # wheel2.moveTo(240,190) # pass def show_animation(): # Implement this function. global car global wheel for i in range(100): car.move(2,0) car.rotate(5) sleep(0.01) sleep(0.5) car.state="fast" print(car.state) for i in range(200): car.move(7,0) car.rotate(10) sleep(0.01) pass car = Car() show_animation()
2
4
TA
Next, please explain the last task 5
from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... tim = Robot() tim.set_trace('blue') def turn_right(): for i in range(3): tim.turn_left() def go_one_line(): while tim.front_is_clear(): tim.move() def turning_point1(): tim.turn_left() tim.move() tim.turn_left() def turning_point2(): turn_right() tim.move() turn_right() tim.turn_left() go_one_line() while tim.right_is_clear(): turning_point2() go_one_line() if tim.left_is_clear(): turning_point1() go_one_line() else: break
17
39
code_explain
We defined a function to move one line and a function to move around, and in order not to collide with the wall, the condition that there is no wall on the side was hung as a while.
cs1qa
null
null
null
null
null
Question: Next, please explain the last task 5 Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... tim = Robot() tim.set_trace('blue') def turn_right(): for i in range(3): tim.turn_left() def go_one_line(): while tim.front_is_clear(): tim.move() def turning_point1(): tim.turn_left() tim.move() tim.turn_left() def turning_point2(): turn_right() tim.move() turn_right() tim.turn_left() go_one_line() while tim.right_is_clear(): turning_point2() go_one_line() if tim.left_is_clear(): turning_point1() go_one_line() else: break
2
0
TA
Is there any reason why you didn't put the two for statements at the bottom of Task1 in the for statement at the top?
from cs1robots import * load_world("worlds/harvest3.wld") hubo=Robot() hubo.set_trace("red") def turn_right(): for i in range(3): hubo.turn_left() def beeper_pick(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() for j in range(2): for i in range(5): beeper_pick() hubo.move() beeper_pick() hubo.turn_left() hubo.move() beeper_pick() hubo.turn_left() for i in range(5): beeper_pick() hubo.move() beeper_pick() turn_right() hubo.move() turn_right() for i in range(5): beeper_pick() hubo.move() beeper_pick() hubo.turn_left() hubo.move() beeper_pick() hubo.turn_left() for i in range(5): beeper_pick() hubo.move()
16
43
reasoning
At the very end, I didn't have to do the work of going up.
cs1qa
null
null
null
null
null
Question: Is there any reason why you didn't put the two for statements at the bottom of Task1 in the for statement at the top? Code: from cs1robots import * load_world("worlds/harvest3.wld") hubo=Robot() hubo.set_trace("red") def turn_right(): for i in range(3): hubo.turn_left() def beeper_pick(): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() for j in range(2): for i in range(5): beeper_pick() hubo.move() beeper_pick() hubo.turn_left() hubo.move() beeper_pick() hubo.turn_left() for i in range(5): beeper_pick() hubo.move() beeper_pick() turn_right() hubo.move() turn_right() for i in range(5): beeper_pick() hubo.move() beeper_pick() hubo.turn_left() hubo.move() beeper_pick() hubo.turn_left() for i in range(5): beeper_pick() hubo.move()
4
4
TA
x = float(i) / 40.0 * 2 * pi t=40*sin(x)+40 You asked me this way, but please briefly explain the reason~~
import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi t=40*sin(x)+40 character_count_per_line = int(t) output_str = ' ' * character_count_per_line+'#' print (output_str)
6
7
reasoning
Since real numbers contain integers, I just used floats. I tried int a while ago and it's okay
cs1qa
null
null
null
null
null
Question: x = float(i) / 40.0 * 2 * pi t=40*sin(x)+40 You asked me this way, but please briefly explain the reason~~ Code: import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi t=40*sin(x)+40 character_count_per_line = int(t) output_str = ' ' * character_count_per_line+'#' print (output_str)
5
0
TA
Please briefly explain when to use global!
balance=0 def deposit(money) : global balance # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! balance=balance+money ################# def withdrawal(money) : global balance # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! if balance>money: balance=balance-money else: print("But you only have",balance,'won') ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! if process=='d': money=int(input('How much do you want to deposit?')) print('You deposit', money, 'won') deposit(money) elif process=='w': money=int(input('How much do you want to withdrawal?')) print("You've withdraw", money, 'won') withdrawal(money) elif process=='c': print("Your current balance is", balance, "won") elif process=='': return else: print("Please, press d or w or c or return") ################# bank()
2
31
variable
Global is a global variable, and when used in two or more functions, we know that global is used.
cs1qa
null
null
null
null
null
Question: Please briefly explain when to use global! Code: balance=0 def deposit(money) : global balance # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! balance=balance+money ################# def withdrawal(money) : global balance # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! if balance>money: balance=balance-money else: print("But you only have",balance,'won') ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! if process=='d': money=int(input('How much do you want to deposit?')) print('You deposit', money, 'won') deposit(money) elif process=='w': money=int(input('How much do you want to withdrawal?')) print("You've withdraw", money, 'won') withdrawal(money) elif process=='c': print("Your current balance is", balance, "won") elif process=='': return else: print("Please, press d or w or c or return") ################# bank()
5
0
TA
And if it is an empty string, why can I use break instead of return?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance=balance+money print("You deposited %d won"%balance) # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance if balance<money: print("You've withdrawn %d won"%money) print("But you only have %d won"%balance) return None balance=balance-money print("You've withdrawn %d won"%money) ################# ### implement ### ################# # Do something on here ! pass ################# def check(): global balance print("Your current balance is %d won"%balance) def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': break elif process== 'd': money=input("How much do you want to deposit?") deposit(int(money)) elif process== 'w': money=input("How much do you want to withdraw?") withdrawal(int(money)) elif process== 'c': check() else: print("Please, press d or w or c or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
45
47
reasoning
The purpose of using break was to end the function by ending the loop in the bank function, and since return is also to terminate the function and return a specific value, I thought that it can be used for the purpose of terminating the function.
cs1qa
null
null
null
null
null
Question: And if it is an empty string, why can I use break instead of return? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance=balance+money print("You deposited %d won"%balance) # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance if balance<money: print("You've withdrawn %d won"%money) print("But you only have %d won"%balance) return None balance=balance-money print("You've withdrawn %d won"%money) ################# ### implement ### ################# # Do something on here ! pass ################# def check(): global balance print("Your current balance is %d won"%balance) def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': break elif process== 'd': money=input("How much do you want to deposit?") deposit(int(money)) elif process== 'w': money=input("How much do you want to withdraw?") withdrawal(int(money)) elif process== 'c': check() else: print("Please, press d or w or c or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
6
2
TA
Can you briefly explain the role of correct_list?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ # 3-2-2. rewrite the condition for visualization. if i in correct_list: ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### # return False for i in range(len(correct_list)): if correct_list[i]==num1: return False elif correct_list[i]==num2: return False if num1==num2: return False if num1<0 or num1>23: return False if num2<0 or num2>23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1]==cards[num2][1]: # correct_list.append(num1, num2) return True else: correct_list.remove(num1) correct_list.remove(num2) print_cards() return False initialize() print_cards() correct_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] print_cards() time.sleep(1) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries%10==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries =tries+1 ###########################################################################
13
13
variable
This is a list of cards with the same drawing side in order. When the list is filled with 24 cards, it means that all pairs have been found.
cs1qa
null
null
null
null
null
Question: Can you briefly explain the role of correct_list? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ # 3-2-2. rewrite the condition for visualization. if i in correct_list: ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### # return False for i in range(len(correct_list)): if correct_list[i]==num1: return False elif correct_list[i]==num2: return False if num1==num2: return False if num1<0 or num1>23: return False if num2<0 or num2>23: return False else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1]==cards[num2][1]: # correct_list.append(num1, num2) return True else: correct_list.remove(num1) correct_list.remove(num2) print_cards() return False initialize() print_cards() correct_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] print_cards() time.sleep(1) correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries%10==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries =tries+1 ###########################################################################
4
0
TA
Please tell me the name of the function you are adding in Task1!
from cs1robots import * # load_world("worlds/add1.wld") # load_world("worlds/add2.wld") load_world("worlds/add34.wld") hubo=Robot() def turn_right(): for i in range (3): hubo.turn_left() hubo.turn_left() hubo.move() turn_right() def run1(): for i in range(9): hubo.move() def check1(): x=0 while hubo.on_beeper(): hubo.pick_beeper() x=x+1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop1(): (x1,x10)=check1() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() def check2(): x=0 turn_right() hubo.move() hubo.turn_left() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop2(): (x1,x10)=check2() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() run1() drop1() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2()
20
58
variable
Whenever the beeper was picked up using the check function, the variable value was added by 1, and //10 was used to solve the case where the sum of the beepers exceeded 10.
cs1qa
null
null
null
null
null
Question: Please tell me the name of the function you are adding in Task1! Code: from cs1robots import * # load_world("worlds/add1.wld") # load_world("worlds/add2.wld") load_world("worlds/add34.wld") hubo=Robot() def turn_right(): for i in range (3): hubo.turn_left() hubo.turn_left() hubo.move() turn_right() def run1(): for i in range(9): hubo.move() def check1(): x=0 while hubo.on_beeper(): hubo.pick_beeper() x=x+1 turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop1(): (x1,x10)=check1() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() def check2(): x=0 turn_right() hubo.move() hubo.turn_left() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 hubo.move() while hubo.on_beeper(): hubo.pick_beeper() x=x+1 x1=x%10 x10=x//10 return(x1,x10) def drop2(): (x1,x10)=check2() turn_right() for i in range(x1): hubo.drop_beeper() hubo.move() for i in range(x10): hubo.drop_beeper() run1() drop1() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2() if hubo.on_beeper(): drop2()
9
1
TA
You created two tasks by using a class. So, what do you think is the advantage of class?
import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') class Card: pass def create_deck(number = 1): deck = [] for i in range(4): for j in range(13): suit = suit_names[i] face = face_names[j] val = value[j] card = Card() card.suit = suit card.face = face card.value = val card.state = True card.image = Image(img_path+suit+"_"+face+".png") deck.append(card) random.shuffle(deck) return deck """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ def hand_value(hand): result = 0 for x in hand: result += x.value return result """ hand is a list including card objects Compute the value of the cards in the list "hand" """ def card_string(card): article = "a " if card.face in [8,"Ace"]: article = "an " return article+card.face+" of "+card.suit """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ def ask_yesno(prompt): while True: answer = input(prompt) if answer == "y": return True elif answer == "n": return False else: print("I beg your pardon!") """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ bj_board.clear() depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for x in dealer: if x.state: x.image.moveTo(x0,y0) x.image.setDepth(depth) bj_board.add(x.image) else: blank = Image(img_path+"Back.png") blank.moveTo(x0,y0) blank.setDepth(depth) bj_board.add(blank) x0 += 40 depth -= 1 x0 = 500 text_dealer = Text("The dealer's Total : "+str(hand_value(dealer)), 10, Point(x0, y0)) bj_board.add(text_dealer) depth=100 for x in player: if x.state: x.image.moveTo(x1,y1) x.image.setDepth(depth) bj_board.add(x.image) else: blank = Image(img_path+"Back.png") blank.moveTo(x1,y1) blank.setDepth(depth) bj_board.add(blank) x1 += 40 depth-=1 x1 = 500 text_player = Text("Your Total : "+str(hand_value(player)), 10, Point(x1, y1)) bj_board.add(text_player) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
14
34
reasoning
I think the advantage of being able to access the corresponding values more intuitively than a list using an index is
cs1qa
null
null
null
null
null
Question: You created two tasks by using a class. So, what do you think is the advantage of class? Code: import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') class Card: pass def create_deck(number = 1): deck = [] for i in range(4): for j in range(13): suit = suit_names[i] face = face_names[j] val = value[j] card = Card() card.suit = suit card.face = face card.value = val card.state = True card.image = Image(img_path+suit+"_"+face+".png") deck.append(card) random.shuffle(deck) return deck """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ def hand_value(hand): result = 0 for x in hand: result += x.value return result """ hand is a list including card objects Compute the value of the cards in the list "hand" """ def card_string(card): article = "a " if card.face in [8,"Ace"]: article = "an " return article+card.face+" of "+card.suit """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ def ask_yesno(prompt): while True: answer = input(prompt) if answer == "y": return True elif answer == "n": return False else: print("I beg your pardon!") """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ bj_board.clear() depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for x in dealer: if x.state: x.image.moveTo(x0,y0) x.image.setDepth(depth) bj_board.add(x.image) else: blank = Image(img_path+"Back.png") blank.moveTo(x0,y0) blank.setDepth(depth) bj_board.add(blank) x0 += 40 depth -= 1 x0 = 500 text_dealer = Text("The dealer's Total : "+str(hand_value(dealer)), 10, Point(x0, y0)) bj_board.add(text_dealer) depth=100 for x in player: if x.state: x.image.moveTo(x1,y1) x.image.setDepth(depth) bj_board.add(x.image) else: blank = Image(img_path+"Back.png") blank.moveTo(x1,y1) blank.setDepth(depth) bj_board.add(blank) x1 += 40 depth-=1 x1 = 500 text_player = Text("Your Total : "+str(hand_value(player)), 10, Point(x1, y1)) bj_board.add(text_player) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
6
2
TA
What does initialize do?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if tries==1: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. tf=0 for i in range(len(correct_list)): if correct_list[i]==num1 or correct_list[i]==num2: tf=1 if num1==num2: tf=1 if num1>23 or num2>23 or num1<0 or num2<0: tf=1 if tf==1: return False ########################################################################### else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1 or i==num2 or i in correct_list: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1]==cards[num2][1]: correct_list.append(num1) correct_list.append(num2) return True else: canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i in correct_list: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not len(correct_list)==24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries%10==1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries%10==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif tries%10==3: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries=tries+1 ###########################################################################
16
35
variable
initialize is a function that randomly mixes the faces and backs of cards to make a game
cs1qa
null
null
null
null
null
Question: What does initialize do? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if tries==1: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. tf=0 for i in range(len(correct_list)): if correct_list[i]==num1 or correct_list[i]==num2: tf=1 if num1==num2: tf=1 if num1>23 or num2>23 or num1<0 or num2<0: tf=1 if tf==1: return False ########################################################################### else: return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1 or i==num2 or i in correct_list: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1]==cards[num2][1]: correct_list.append(num1) correct_list.append(num2) return True else: canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i in correct_list: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not len(correct_list)==24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if tries%10==1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif tries%10==2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif tries%10==3: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries=tries+1 ###########################################################################
6
0
TA
What is the sum of
def fibonacci(upper_bound): a = [0, 1] i=0 while (a[i+1]+ a[i]) < upper_bound: x = a[i+1]+ a[i] a.append(x) i= i+1 return a print(fibonacci(1000))
3
4
variable
Two consecutive numbers in the list
cs1qa
null
null
null
null
null
Question: What is the sum of Code: def fibonacci(upper_bound): a = [0, 1] i=0 while (a[i+1]+ a[i]) < upper_bound: x = a[i+1]+ a[i] a.append(x) i= i+1 return a print(fibonacci(1000))
4
2
TA
What is the unit of x in sin(x) in task3?
import math sin = math.sin pi = math.pi a = int (input ('How many steps? ')) for i in range(a): x = float(i) / (a-1) * 2 * pi print (sin(x))
8
9
variable
Radians
cs1qa
null
null
null
null
null
Question: What is the unit of x in sin(x) in task3? Code: import math sin = math.sin pi = math.pi a = int (input ('How many steps? ')) for i in range(a): x = float(i) / (a-1) * 2 * pi print (sin(x))
5
0
TA
Please explain how you used the global variable in task 1!
balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! global balance balance = balance + money print("You deposited",money,"won") ################# def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! global balance if money > balance: print("You've withdrawn",money,"won") print("But you only have",balance,"won") else: balance = balance - money print("You've withdraw",money,"won") ################# def bank(): # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! if process == '': return elif process == 'd': dmoney = int(input("How much do you want to deposit? ")) deposit(dmoney) elif process == 'w': wmoney = int(input("How much do you want to withdraw? ")) withdrawal(wmoney) elif process == 'c': print("Your current balance is",balance,"won") else: print("Please, press d or w or c or return") ################# bank()
0
37
variable
By specifying the balance as a global variable, all variables can be used in each function, and this is called the bank's remaining money.
cs1qa
null
null
null
null
null
Question: Please explain how you used the global variable in task 1! Code: balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! global balance balance = balance + money print("You deposited",money,"won") ################# def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! global balance if money > balance: print("You've withdrawn",money,"won") print("But you only have",balance,"won") else: balance = balance - money print("You've withdraw",money,"won") ################# def bank(): # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! if process == '': return elif process == 'd': dmoney = int(input("How much do you want to deposit? ")) deposit(dmoney) elif process == 'w': wmoney = int(input("How much do you want to withdraw? ")) withdrawal(wmoney) elif process == 'c': print("Your current balance is",balance,"won") else: print("Please, press d or w or c or return") ################# bank()
1
3
TA
Task4 What algorithm is described Please explain briefly!
from cs1robots import * # create_world() # load_world('worlds/world_file_name.wld') load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace('blue') def turnright(): for i in range(3): hubo.turn_left() def wentandpick(): hubo.move() hubo.pick_beeper() def turninright(): turnright() hubo.move() hubo.pick_beeper() turnright() def turninleft(): hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() def oneturn(): for i in range(5): wentandpick() def twoconer(): oneturn() turninleft() oneturn() turninright() wentandpick() for i in range(2): twoconer() oneturn() turninleft() oneturn()
0
35
code_explain
In task 4 harvest, the beeper is 6*6, and the algorithm is made by the robot picking up the beeper one by one, and the beeper right in front of the robot is used as the starting point and proceeded similarly to the number 2 when viewed from above!
cs1qa
null
null
null
null
null
Question: Task4 What algorithm is described Please explain briefly! Code: from cs1robots import * # create_world() # load_world('worlds/world_file_name.wld') load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace('blue') def turnright(): for i in range(3): hubo.turn_left() def wentandpick(): hubo.move() hubo.pick_beeper() def turninright(): turnright() hubo.move() hubo.pick_beeper() turnright() def turninleft(): hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() def oneturn(): for i in range(5): wentandpick() def twoconer(): oneturn() turninleft() oneturn() turninright() wentandpick() for i in range(2): twoconer() oneturn() turninleft() oneturn()
2
0
TA
Please briefly explain how to pick up the beeper in step 1 along with the function name.
from cs1robots import* load_world('worlds/harvest3.wld') hubo=Robot(beepers=0) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def uturn_left(): hubo.turn_left() hubo.move() hubo.turn_left() def uturn_right(): turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() turn_right() def cycle(): while hubo.front_is_clear(): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() uturn_left() for i in range (5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() uturn_right() for i in range (3): cycle()
22
36
code_explain
When going to the left, the while statement is used to move to the end, and when going to the right, the for statement is used to proceed 5 times.
cs1qa
null
null
null
null
null
Question: Please briefly explain how to pick up the beeper in step 1 along with the function name. Code: from cs1robots import* load_world('worlds/harvest3.wld') hubo=Robot(beepers=0) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def uturn_left(): hubo.turn_left() hubo.move() hubo.turn_left() def uturn_right(): turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() turn_right() def cycle(): while hubo.front_is_clear(): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() uturn_left() for i in range (5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() uturn_right() for i in range (3): cycle()
4
3
TA
Could you explain why you set the character_count_per_line value like that
import math sin = math.sin pi = math.pi for i in range(41): x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40 * (sin(x) + 1)) # Change this line to print out sine curve correctly. output_str = '#' * character_count_per_line print (output_str)
7
7
reasoning
The number of shops in a line was set to be similar to the linear function in that it was necessary to print 0 shops when sin=-1, 40 shops when 0, and 80 shops when it was 1.
cs1qa
null
null
null
null
null
Question: Could you explain why you set the character_count_per_line value like that Code: import math sin = math.sin pi = math.pi for i in range(41): x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40 * (sin(x) + 1)) # Change this line to print out sine curve correctly. output_str = '#' * character_count_per_line print (output_str)
2
0
TA
Please briefly explain the turning function!
from cs1robots import * load_world("worlds/harvest3.wld") hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def move_check(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def straight(): for i in range(5): move_check() def turning(): if hubo.front_is_clear(): turn_right() move_check() turn_right() else: hubo.turn_left() move_check() hubo.turn_left() hubo.move() hubo.pick_beeper() for i in range(6): straight() turning()
19
27
variable
Turning turns counterclockwise when the front is blocked, and turns clockwise when it is not blocked!!
cs1qa
null
null
null
null
null
Question: Please briefly explain the turning function! Code: from cs1robots import * load_world("worlds/harvest3.wld") hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def move_check(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def straight(): for i in range(5): move_check() def turning(): if hubo.front_is_clear(): turn_right() move_check() turn_right() else: hubo.turn_left() move_check() hubo.turn_left() hubo.move() hubo.pick_beeper() for i in range(6): straight() turning()
1
0
TA
Could you explain what kind of implication is the go_and_back function?
from cs1robots import* create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def go_straight(): for i in range(9): hubo.move() def go_and_back(): go_straight() turn_right() hubo.move() turn_right() go_straight() hubo.turn_left() for i in range(4): go_and_back() hubo.turn_left() hubo.move() hubo.turn_left() go_and_back()
14
19
variable
It is a function that goes up and down, and goes 2 lines
cs1qa
null
null
null
null
null
Question: Could you explain what kind of implication is the go_and_back function? Code: from cs1robots import* create_world() hubo=Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def go_straight(): for i in range(9): hubo.move() def go_and_back(): go_straight() turn_right() hubo.move() turn_right() go_straight() hubo.turn_left() for i in range(4): go_and_back() hubo.turn_left() hubo.move() hubo.turn_left() go_and_back()
8
0
TA
Why do you add 1 in every loop in the for function?
from time import sleep def merge(input_filenames, output_filename): # Implement here # ... pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
null
null
reasoning
Each time a line is read, we add 1 to increase the year by 1.
cs1qa
null
null
null
null
null
Question: Why do you add 1 in every loop in the for function? Code: from time import sleep def merge(input_filenames, output_filename): # Implement here # ... pass merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
8
1
TA
How did you write the code to print the country name when you input the country code at the end?
f=open("average-latitude-longitude-countries.csv","r") r=[] r1=[] r2=[] for l in f: r.append(l) r=r[1:] for i in range(len(r)): a=r[i].split(",") if len(a)>4: code=a[0][1:-1] name=a[1]+","+a[2] name=name[1:-1] lat=a[-2] lon=a[-1] else: code=a[0][1:-1] name=a[1][1:-1] lat=a[2] lon=a[3] r1.append((str(code),str(name))) r2.append((str(code),(float(lat),float(lon)))) print(r1) print(r2) for i in range(len(r2)): if r2[i][1][0]<0: print(r1[i][1]) a=input('Enter country code: ') for i in range(len(r1)): if r1[i][0]==a: print(r1[i][1])
34
37
code_explain
Since the country name and code are stored in the r1 list, if the code is the same, the country name corresponding to it is displayed.
cs1qa
null
null
null
null
null
Question: How did you write the code to print the country name when you input the country code at the end? Code: f=open("average-latitude-longitude-countries.csv","r") r=[] r1=[] r2=[] for l in f: r.append(l) r=r[1:] for i in range(len(r)): a=r[i].split(",") if len(a)>4: code=a[0][1:-1] name=a[1]+","+a[2] name=name[1:-1] lat=a[-2] lon=a[-1] else: code=a[0][1:-1] name=a[1][1:-1] lat=a[2] lon=a[3] r1.append((str(code),str(name))) r2.append((str(code),(float(lat),float(lon)))) print(r1) print(r2) for i in range(len(r2)): if r2[i][1][0]<0: print(r1[i][1]) a=input('Enter country code: ') for i in range(len(r1)): if r1[i][0]==a: print(r1[i][1])
2
0
TA
What is task1's one_cycle function doing?
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def one_length(): for i in range(5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def one_cycle(): one_length() hubo.turn_left() hubo.move() hubo.turn_left() one_length() hubo.move() for i in range(2): one_cycle() turn_right() hubo.move() turn_right() one_cycle()
18
23
variable
It is a function that goes back and forth once. It moves horizontally and then turns, and then it moves horizontally and then turns.
cs1qa
null
null
null
null
null
Question: What is task1's one_cycle function doing? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def one_length(): for i in range(5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def one_cycle(): one_length() hubo.turn_left() hubo.move() hubo.turn_left() one_length() hubo.move() for i in range(2): one_cycle() turn_right() hubo.move() turn_right() one_cycle()
3
0
TA
What's the reason you rotate the gun 4 times in the first task?
from cs1robots import * create_world() hubo = Robot(orientation = 'W', avenue=7, street=5) for i in range(4): if not hubo.facing_north(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
4
6
reasoning
In order to get north in four turns. I think it's okay to change it up to three times
cs1qa
null
null
null
null
null
Question: What's the reason you rotate the gun 4 times in the first task? Code: from cs1robots import * create_world() hubo = Robot(orientation = 'W', avenue=7, street=5) for i in range(4): if not hubo.facing_north(): hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
3
2
TA
What do the av and st variables do?
from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo=Robot() hubo.set_trace('blue') av=0 st=0 def turn_right(): for i in range (3): hubo.turn_left() while(hubo.front_is_clear()): hubo.move() av=av+1 hubo.turn_left() hubo.turn_left() for i in range (av): hubo.move() turn_right() while(hubo.front_is_clear()): hubo.move() st=st+1 hubo.turn_left() hubo.turn_left() for i in range (st): hubo.move() hubo.turn_left() def horizontalMove(): for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def goup(): hubo.turn_left() hubo.move() hubo.turn_left() for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.turn_left() for j in range (st): horizontalMove() goup() horizontalMove() turn_right() while hubo.front_is_clear(): hubo.move() turn_right() while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left()
15
26
variable
av is the number of horizontal cells -1, and st is the number of vertical cells -1.
cs1qa
null
null
null
null
null
Question: What do the av and st variables do? Code: from cs1robots import * # Your code must work with any of the world files below. load_world('worlds/trash3.wld') # load_world('worlds/trash4.wld') hubo=Robot() hubo.set_trace('blue') av=0 st=0 def turn_right(): for i in range (3): hubo.turn_left() while(hubo.front_is_clear()): hubo.move() av=av+1 hubo.turn_left() hubo.turn_left() for i in range (av): hubo.move() turn_right() while(hubo.front_is_clear()): hubo.move() st=st+1 hubo.turn_left() hubo.turn_left() for i in range (st): hubo.move() hubo.turn_left() def horizontalMove(): for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def goup(): hubo.turn_left() hubo.move() hubo.turn_left() for i in range (av): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.turn_left() for j in range (st): horizontalMove() goup() horizontalMove() turn_right() while hubo.front_is_clear(): hubo.move() turn_right() while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left()
3
1
TA
Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper.
from cs1robots import * # Your code must work for all world files below. #load_world( "worlds/trash1.wld" ) load_world( "worlds/trash2.wld" ) hubo = Robot() def move_pick(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def turn_around(): for i in range(2): hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def move_across(): while hubo.front_is_clear(): move_pick() move_across() turn_around() move_across() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn_around() hubo.move() hubo.turn_left()
10
11
reasoning
Since the number of beeper is different for each location, here we define move_pick by using while until the beeper disappears.
cs1qa
null
null
null
null
null
Question: Please also briefly explain the reason why you should use the while statement, not the for statement loop, in the process of picking up the beeper. Code: from cs1robots import * # Your code must work for all world files below. #load_world( "worlds/trash1.wld" ) load_world( "worlds/trash2.wld" ) hubo = Robot() def move_pick(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def turn_around(): for i in range(2): hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def move_across(): while hubo.front_is_clear(): move_pick() move_across() turn_around() move_across() turn_right() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() turn_around() hubo.move() hubo.turn_left()
3
0
TA
What is the orient function in task1?
from cs1robots import * create_world(avenues = 4, streets = 7) hubo=Robot(orientation = 'W', avenue = 3, street = 5) hubo.set_trace('blue') def orient(): while hubo.facing_north() == False: hubo.turn_left() hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() orient() move()
5
8
variable
I have set up the code to make the direction look east.
cs1qa
null
null
null
null
null
Question: What is the orient function in task1? Code: from cs1robots import * create_world(avenues = 4, streets = 7) hubo=Robot(orientation = 'W', avenue = 3, street = 5) hubo.set_trace('blue') def orient(): while hubo.facing_north() == False: hubo.turn_left() hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() orient() move()
10
0
TA
What is the rabbit's ribbon function?
from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor((112,163,61)) grass.setBorderColor((112,163,214)) grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000, 1000, Point(250, 250)) sky.setFillColor((112,163,214)) sky.setDepth(110) _scene.add(sky) cloud = Layer() circle1 = Circle(20,Point(0,-6)) circle1.setFillColor('white') circle1.setBorderColor('white') cloud.add(circle1) circle2 = Circle(20,Point(6,6)) circle2.setFillColor('white') circle2.setBorderColor('white') cloud.add(circle2) circle3 = Circle(15,Point(-16,-3)) circle3.setFillColor('white') circle3.setBorderColor('white') cloud.add(circle3) circle4 = Circle(17,Point(25,0)) circle4.setFillColor('white') circle4.setBorderColor('white') cloud.add(circle4) circle5 = Circle(15,Point(35,0)) circle5.setFillColor('white') circle5.setBorderColor('white') cloud.add(circle5) cloud.setDepth(100) cloud.move(100,50) _scene.add(cloud) cloud2=cloud.clone() cloud2.scale(1.3) cloud2.move(180,60) _scene.add(cloud2) cloud3=cloud.clone() cloud3.scale(0.9) cloud3.move(300,10) _scene.add(cloud3) pass """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ class Rabbit (object): def __init__(self,color='pink',ribbon=False): self.color=color self.ribbon=ribbon rabbit=Layer() ear=Layer() ear1 = Ellipse(8,30,Point(-10,-50)) ear1.setFillColor(color) ear1.setBorderColor(color) ear1.rotate(343) ear1.adjustReference(0,15) ear.add(ear1) ear2=ear1.clone() ear2.rotate(34) ear2.move(10,0) ear.add(ear2) rabbit.add(ear) face=Ellipse(28,25,Point(0,-30)) face.setFillColor(color) face.setBorderColor(color) rabbit.add(face) body = Ellipse(47,26,Point(-10,-10)) body.setFillColor(color) body.setBorderColor(color) rabbit.add(body) rabbit.setDepth(80) rabbit.move(100,170) leg=Layer() leg1 = Circle(6,Point(5,5)) leg1.setFillColor(color) leg1.setBorderColor(color) leg.add(leg1) leg2 = leg1.clone() leg2.move(-30,0) leg.add(leg2) rabbit.add(leg) tail = Circle(7,Point(-35,-14)) tail.setFillColor(color) tail.setBorderColor(color) rabbit.add(tail) eye1 = Circle(2,Point(9,-32)) eye1.setFillColor('black') rabbit.add(eye1) eye2 = Circle(2,Point(-3,-32)) eye2.setFillColor('black') rabbit.add(eye2) mouth = Polygon(Point(3,-28),Point(5,-24),Point(1,-24)) mouth.setFillColor('red') mouth.setBorderColor('red') rabbit.add(mouth) self.layer=rabbit self.leg=leg self.ear=ear _scene.add(self.layer) if ribbon==True: self.Ribbon() self.count = 0 def leg_and_ear_move(self): if self.count % 3 == 0: self.leg.move(0, 3) self.ear.rotate(7) elif self.count % 3 == 1: self.leg.move(0,-5) self.ear.rotate(-7) else: self.leg.move(0,2) self.count += 1 if self.count % 3 == 0: self.count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def Ribbon(self): for i in range(180): self.layer.rotate(2) self.layer.scale(1.002) sleep(0.001) create_world() rabbit = Rabbit() # define your objects, e.g. mario = Mario('blue', 'normal') # write your animation scenario here for i in range(150): rabbit.move(1,0) rabbit.leg_and_ear_move() rabbit.Ribbon() for i in range(150): rabbit.move(2,0) rabbit.leg_and_ear_move()
192
196
variable
This is a function that increases the size of the rabbit while turning around
cs1qa
null
null
null
null
null
Question: What is the rabbit's ribbon function? Code: from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor((112,163,61)) grass.setBorderColor((112,163,214)) grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000, 1000, Point(250, 250)) sky.setFillColor((112,163,214)) sky.setDepth(110) _scene.add(sky) cloud = Layer() circle1 = Circle(20,Point(0,-6)) circle1.setFillColor('white') circle1.setBorderColor('white') cloud.add(circle1) circle2 = Circle(20,Point(6,6)) circle2.setFillColor('white') circle2.setBorderColor('white') cloud.add(circle2) circle3 = Circle(15,Point(-16,-3)) circle3.setFillColor('white') circle3.setBorderColor('white') cloud.add(circle3) circle4 = Circle(17,Point(25,0)) circle4.setFillColor('white') circle4.setBorderColor('white') cloud.add(circle4) circle5 = Circle(15,Point(35,0)) circle5.setFillColor('white') circle5.setBorderColor('white') cloud.add(circle5) cloud.setDepth(100) cloud.move(100,50) _scene.add(cloud) cloud2=cloud.clone() cloud2.scale(1.3) cloud2.move(180,60) _scene.add(cloud2) cloud3=cloud.clone() cloud3.scale(0.9) cloud3.move(300,10) _scene.add(cloud3) pass """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ class Rabbit (object): def __init__(self,color='pink',ribbon=False): self.color=color self.ribbon=ribbon rabbit=Layer() ear=Layer() ear1 = Ellipse(8,30,Point(-10,-50)) ear1.setFillColor(color) ear1.setBorderColor(color) ear1.rotate(343) ear1.adjustReference(0,15) ear.add(ear1) ear2=ear1.clone() ear2.rotate(34) ear2.move(10,0) ear.add(ear2) rabbit.add(ear) face=Ellipse(28,25,Point(0,-30)) face.setFillColor(color) face.setBorderColor(color) rabbit.add(face) body = Ellipse(47,26,Point(-10,-10)) body.setFillColor(color) body.setBorderColor(color) rabbit.add(body) rabbit.setDepth(80) rabbit.move(100,170) leg=Layer() leg1 = Circle(6,Point(5,5)) leg1.setFillColor(color) leg1.setBorderColor(color) leg.add(leg1) leg2 = leg1.clone() leg2.move(-30,0) leg.add(leg2) rabbit.add(leg) tail = Circle(7,Point(-35,-14)) tail.setFillColor(color) tail.setBorderColor(color) rabbit.add(tail) eye1 = Circle(2,Point(9,-32)) eye1.setFillColor('black') rabbit.add(eye1) eye2 = Circle(2,Point(-3,-32)) eye2.setFillColor('black') rabbit.add(eye2) mouth = Polygon(Point(3,-28),Point(5,-24),Point(1,-24)) mouth.setFillColor('red') mouth.setBorderColor('red') rabbit.add(mouth) self.layer=rabbit self.leg=leg self.ear=ear _scene.add(self.layer) if ribbon==True: self.Ribbon() self.count = 0 def leg_and_ear_move(self): if self.count % 3 == 0: self.leg.move(0, 3) self.ear.rotate(7) elif self.count % 3 == 1: self.leg.move(0,-5) self.ear.rotate(-7) else: self.leg.move(0,2) self.count += 1 if self.count % 3 == 0: self.count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def Ribbon(self): for i in range(180): self.layer.rotate(2) self.layer.scale(1.002) sleep(0.001) create_world() rabbit = Rabbit() # define your objects, e.g. mario = Mario('blue', 'normal') # write your animation scenario here for i in range(150): rabbit.move(1,0) rabbit.leg_and_ear_move() rabbit.Ribbon() for i in range(150): rabbit.move(2,0) rabbit.leg_and_ear_move()
10
0
TA
What is the rabbit's ribbon function?
from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor((112,163,61)) grass.setBorderColor((112,163,214)) grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000, 1000, Point(250, 250)) sky.setFillColor((112,163,214)) sky.setDepth(110) _scene.add(sky) cloud = Layer() circle1 = Circle(20,Point(0,-6)) circle1.setFillColor('white') circle1.setBorderColor('white') cloud.add(circle1) circle2 = Circle(20,Point(6,6)) circle2.setFillColor('white') circle2.setBorderColor('white') cloud.add(circle2) circle3 = Circle(15,Point(-16,-3)) circle3.setFillColor('white') circle3.setBorderColor('white') cloud.add(circle3) circle4 = Circle(17,Point(25,0)) circle4.setFillColor('white') circle4.setBorderColor('white') cloud.add(circle4) circle5 = Circle(15,Point(35,0)) circle5.setFillColor('white') circle5.setBorderColor('white') cloud.add(circle5) cloud.setDepth(100) cloud.move(100,50) _scene.add(cloud) cloud2=cloud.clone() cloud2.scale(1.3) cloud2.move(180,60) _scene.add(cloud2) cloud3=cloud.clone() cloud3.scale(0.9) cloud3.move(300,10) _scene.add(cloud3) pass """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ class Rabbit (object): def __init__(self,color='pink',ribbon=False): self.color=color self.ribbon=ribbon rabbit=Layer() ear=Layer() ear1 = Ellipse(8,30,Point(-10,-50)) ear1.setFillColor(color) ear1.setBorderColor(color) ear1.rotate(343) ear1.adjustReference(0,15) ear.add(ear1) ear2=ear1.clone() ear2.rotate(34) ear2.move(10,0) ear.add(ear2) rabbit.add(ear) face=Ellipse(28,25,Point(0,-30)) face.setFillColor(color) face.setBorderColor(color) rabbit.add(face) body = Ellipse(47,26,Point(-10,-10)) body.setFillColor(color) body.setBorderColor(color) rabbit.add(body) rabbit.setDepth(80) rabbit.move(100,170) leg=Layer() leg1 = Circle(6,Point(5,5)) leg1.setFillColor(color) leg1.setBorderColor(color) leg.add(leg1) leg2 = leg1.clone() leg2.move(-30,0) leg.add(leg2) rabbit.add(leg) tail = Circle(7,Point(-35,-14)) tail.setFillColor(color) tail.setBorderColor(color) rabbit.add(tail) eye1 = Circle(2,Point(9,-32)) eye1.setFillColor('black') rabbit.add(eye1) eye2 = Circle(2,Point(-3,-32)) eye2.setFillColor('black') rabbit.add(eye2) mouth = Polygon(Point(3,-28),Point(5,-24),Point(1,-24)) mouth.setFillColor('red') mouth.setBorderColor('red') rabbit.add(mouth) self.layer=rabbit self.leg=leg self.ear=ear _scene.add(self.layer) if ribbon==True: self.Ribbon() self.count = 0 def leg_and_ear_move(self): if self.count % 3 == 0: self.leg.move(0, 3) self.ear.rotate(7) elif self.count % 3 == 1: self.leg.move(0,-5) self.ear.rotate(-7) else: self.leg.move(0,2) self.count += 1 if self.count % 3 == 0: self.count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def Ribbon(self): for i in range(180): self.layer.rotate(2) self.layer.scale(1.002) sleep(0.001) create_world() rabbit = Rabbit() # define your objects, e.g. mario = Mario('blue', 'normal') # write your animation scenario here for i in range(150): rabbit.move(1,0) rabbit.leg_and_ear_move() rabbit.Ribbon() for i in range(150): rabbit.move(2,0) rabbit.leg_and_ear_move()
192
196
variable
This is a function that increases the size of the rabbit while turning around
cs1qa
null
null
null
null
null
Question: What is the rabbit's ribbon function? Code: from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor((112,163,61)) grass.setBorderColor((112,163,214)) grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000, 1000, Point(250, 250)) sky.setFillColor((112,163,214)) sky.setDepth(110) _scene.add(sky) cloud = Layer() circle1 = Circle(20,Point(0,-6)) circle1.setFillColor('white') circle1.setBorderColor('white') cloud.add(circle1) circle2 = Circle(20,Point(6,6)) circle2.setFillColor('white') circle2.setBorderColor('white') cloud.add(circle2) circle3 = Circle(15,Point(-16,-3)) circle3.setFillColor('white') circle3.setBorderColor('white') cloud.add(circle3) circle4 = Circle(17,Point(25,0)) circle4.setFillColor('white') circle4.setBorderColor('white') cloud.add(circle4) circle5 = Circle(15,Point(35,0)) circle5.setFillColor('white') circle5.setBorderColor('white') cloud.add(circle5) cloud.setDepth(100) cloud.move(100,50) _scene.add(cloud) cloud2=cloud.clone() cloud2.scale(1.3) cloud2.move(180,60) _scene.add(cloud2) cloud3=cloud.clone() cloud3.scale(0.9) cloud3.move(300,10) _scene.add(cloud3) pass """ define your own objects, e.g. Mario and Mushroom class Mushroom (object): def __init__(self, x, y): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) mushroom.add(lowermush) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom # save mushroom shape in the class _scene.add(self.layer) # add to global Canvas class Mario (object): def __init__(self, ... self.layer = Layer() ... _scene.add(self.layer) """ class Rabbit (object): def __init__(self,color='pink',ribbon=False): self.color=color self.ribbon=ribbon rabbit=Layer() ear=Layer() ear1 = Ellipse(8,30,Point(-10,-50)) ear1.setFillColor(color) ear1.setBorderColor(color) ear1.rotate(343) ear1.adjustReference(0,15) ear.add(ear1) ear2=ear1.clone() ear2.rotate(34) ear2.move(10,0) ear.add(ear2) rabbit.add(ear) face=Ellipse(28,25,Point(0,-30)) face.setFillColor(color) face.setBorderColor(color) rabbit.add(face) body = Ellipse(47,26,Point(-10,-10)) body.setFillColor(color) body.setBorderColor(color) rabbit.add(body) rabbit.setDepth(80) rabbit.move(100,170) leg=Layer() leg1 = Circle(6,Point(5,5)) leg1.setFillColor(color) leg1.setBorderColor(color) leg.add(leg1) leg2 = leg1.clone() leg2.move(-30,0) leg.add(leg2) rabbit.add(leg) tail = Circle(7,Point(-35,-14)) tail.setFillColor(color) tail.setBorderColor(color) rabbit.add(tail) eye1 = Circle(2,Point(9,-32)) eye1.setFillColor('black') rabbit.add(eye1) eye2 = Circle(2,Point(-3,-32)) eye2.setFillColor('black') rabbit.add(eye2) mouth = Polygon(Point(3,-28),Point(5,-24),Point(1,-24)) mouth.setFillColor('red') mouth.setBorderColor('red') rabbit.add(mouth) self.layer=rabbit self.leg=leg self.ear=ear _scene.add(self.layer) if ribbon==True: self.Ribbon() self.count = 0 def leg_and_ear_move(self): if self.count % 3 == 0: self.leg.move(0, 3) self.ear.rotate(7) elif self.count % 3 == 1: self.leg.move(0,-5) self.ear.rotate(-7) else: self.leg.move(0,2) self.count += 1 if self.count % 3 == 0: self.count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def Ribbon(self): for i in range(180): self.layer.rotate(2) self.layer.scale(1.002) sleep(0.001) create_world() rabbit = Rabbit() # define your objects, e.g. mario = Mario('blue', 'normal') # write your animation scenario here for i in range(150): rabbit.move(1,0) rabbit.leg_and_ear_move() rabbit.Ribbon() for i in range(150): rabbit.move(2,0) rabbit.leg_and_ear_move()
1
4
TA
Please explain the reason for doing the task 5 diagonally.
from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def firstProcess(): turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() for i in range(6): hubo.move() for i in range(3): hubo.pick_beeper() for k in range(5): firstProcess() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() for k in range(5): firstProcess() hubo.move() hubo.turn_left() hubo.move() hubo.turn_left()
19
40
reasoning
For number 5, I thought that going diagonally, if you reverse the direction, you can use the same process and also the closest distance
cs1qa
null
null
null
null
null
Question: Please explain the reason for doing the task 5 diagonally. Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def firstProcess(): turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() for i in range(6): hubo.move() for i in range(3): hubo.pick_beeper() for k in range(5): firstProcess() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() for k in range(5): firstProcess() hubo.move() hubo.turn_left() hubo.move() hubo.turn_left()
5
1
TA
Please explain how you squeezed it!
from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. global tail global canvas global pig global legL global legR canvas=Canvas(800,600) canvas.setBackgroundColor("light blue") ground=Rectangle(800,110,Point(400,540)) ground.setFillColor('brown') canvas.add(ground) pig=Layer() ear=Square(15,Point(400,360)) ear.setFillColor('lightpink') ear.rotate(45) ear.setDepth(20) pig.add(ear) face=Circle(40, Point(400,400)) face.setFillColor('lightpink') face.setDepth(10) pig.add(face) nose=Layer() nose1=Ellipse(30,20,Point(360,400)) nose1.setFillColor('lightpink') nose.add(nose1) nose2=Circle(4,Point(368,400)) nose2.setFillColor('black') nose.add(nose2) nose3=Circle(4,Point(352,400)) nose3.setFillColor('black') nose.add(nose3) nose.setDepth(5) pig.add(nose) body=Ellipse(120,80,Point(480,400)) body.setFillColor('lightpink') body.setDepth(30) pig.add(body) eye=Circle(4,Point(385,380)) eye.setFillColor('black') eye.setDepth(5) pig.add(eye) legL=Layer() leg1=Rectangle(20,100,Point(460,435)) leg1.setFillColor('lightpink') legL.add(leg1) foot1=Square(20,Point(460,480)) foot1.setFillColor('black') legL.add(foot1) legL.setDepth(40) legL.adjustReference(460,440) pig.add(legL) legR=Layer() leg1=Rectangle(20,100,Point(500,435)) leg1.setFillColor('lightpink') legR.add(leg1) foot1=Square(20,Point(500,480)) foot1.setFillColor('black') legR.add(foot1) legR.setDepth(40) legR.adjustReference(460,440) pig.add(legR) tail=Layer() tail1=Rectangle(5,20,Point(530,380)) tail1.setFillColor('lightpink') tail1.rotate(45) tail.add(tail1) tail2=Rectangle(5,20,Point(545,380)) tail2.setFillColor('lightpink') tail2.rotate(135) tail.add(tail2) tail3=Rectangle(5,20,Point(560,380)) tail3.setFillColor('lightpink') tail3.rotate(45) tail.add(tail3) tail.adjustReference(530,380) tail.setDepth(50) pig.add(tail) canvas.add(pig) pass def show_animation(): # Implement this function. global pig global canvas global tail global legL global legR while True: for i in range (45): tail.rotate(1) sleep(0.01) for i in range (45): tail.rotate(-1) sleep(0.01) for i in range (3): for j in range (10): legL.rotate(1) legR.rotate(-1) sleep(0.01) pig.move(-1,0) for j in range (10): legL.rotate(-1) legR.rotate(1) sleep(0.01) pig.move(-1,0) pass draw_animal() show_animation()
3
121
code_explain
In Task 2, the pig's tail and the left leg and the right leg are placed as a layer and move separately, and the entire pig's body is also placed in one layer, and the code is made to move. In the case of the tail and the legs, repeat the rotation by a certain angle in a clockwise counterclockwise direction according to the walking appearance.
cs1qa
null
null
null
null
null
Question: Please explain how you squeezed it! Code: from cs1graphics import * from time import sleep def draw_animal(): # Implement this function. global tail global canvas global pig global legL global legR canvas=Canvas(800,600) canvas.setBackgroundColor("light blue") ground=Rectangle(800,110,Point(400,540)) ground.setFillColor('brown') canvas.add(ground) pig=Layer() ear=Square(15,Point(400,360)) ear.setFillColor('lightpink') ear.rotate(45) ear.setDepth(20) pig.add(ear) face=Circle(40, Point(400,400)) face.setFillColor('lightpink') face.setDepth(10) pig.add(face) nose=Layer() nose1=Ellipse(30,20,Point(360,400)) nose1.setFillColor('lightpink') nose.add(nose1) nose2=Circle(4,Point(368,400)) nose2.setFillColor('black') nose.add(nose2) nose3=Circle(4,Point(352,400)) nose3.setFillColor('black') nose.add(nose3) nose.setDepth(5) pig.add(nose) body=Ellipse(120,80,Point(480,400)) body.setFillColor('lightpink') body.setDepth(30) pig.add(body) eye=Circle(4,Point(385,380)) eye.setFillColor('black') eye.setDepth(5) pig.add(eye) legL=Layer() leg1=Rectangle(20,100,Point(460,435)) leg1.setFillColor('lightpink') legL.add(leg1) foot1=Square(20,Point(460,480)) foot1.setFillColor('black') legL.add(foot1) legL.setDepth(40) legL.adjustReference(460,440) pig.add(legL) legR=Layer() leg1=Rectangle(20,100,Point(500,435)) leg1.setFillColor('lightpink') legR.add(leg1) foot1=Square(20,Point(500,480)) foot1.setFillColor('black') legR.add(foot1) legR.setDepth(40) legR.adjustReference(460,440) pig.add(legR) tail=Layer() tail1=Rectangle(5,20,Point(530,380)) tail1.setFillColor('lightpink') tail1.rotate(45) tail.add(tail1) tail2=Rectangle(5,20,Point(545,380)) tail2.setFillColor('lightpink') tail2.rotate(135) tail.add(tail2) tail3=Rectangle(5,20,Point(560,380)) tail3.setFillColor('lightpink') tail3.rotate(45) tail.add(tail3) tail.adjustReference(530,380) tail.setDepth(50) pig.add(tail) canvas.add(pig) pass def show_animation(): # Implement this function. global pig global canvas global tail global legL global legR while True: for i in range (45): tail.rotate(1) sleep(0.01) for i in range (45): tail.rotate(-1) sleep(0.01) for i in range (3): for j in range (10): legL.rotate(1) legR.rotate(-1) sleep(0.01) pig.move(-1,0) for j in range (10): legL.rotate(-1) legR.rotate(1) sleep(0.01) pig.move(-1,0) pass draw_animal() show_animation()
8
1
TA
What does the reader function do to the result?
from csv import * f=open('average-latitude-longitude-countries.csv','r') rdr=reader(f) list1=[] list2=[] dic={} next(rdr) for line in rdr: print(line) list1.append((line[0],line[1])) list2.append((line[0],(float(line[2]),float(line[3])))) dic[line[0]]=line[1] def act1(): print(list1) print(list2) def act2(): for i in range(len(list2)): if list2[i][1][0]<0: print(list1[i][1]) def act3(): cc=input('Enter country code: ') if cc in dic: print(dic.get(cc)) # act1() # act2() # act3()
3
3
variable
I know that, is divided into divisions
cs1qa
null
null
null
null
null
Question: What does the reader function do to the result? Code: from csv import * f=open('average-latitude-longitude-countries.csv','r') rdr=reader(f) list1=[] list2=[] dic={} next(rdr) for line in rdr: print(line) list1.append((line[0],line[1])) list2.append((line[0],(float(line[2]),float(line[3])))) dic[line[0]]=line[1] def act1(): print(list1) print(list2) def act2(): for i in range(len(list2)): if list2[i][1][0]<0: print(list1[i][1]) def act3(): cc=input('Enter country code: ') if cc in dic: print(dic.get(cc)) # act1() # act2() # act3()
9
0
TA
What elements is the cards list in?
from cs1graphics import * import time class Card(object): def __init__(self, img, name): self.img = img self.name = name self.correct = False def __eq__(self, c1): return self.name == c1.name def __ne__(self, c1): return not (self.name == c1.name) canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_cnt = 0 def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(Card(img, names[i])) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list import random random.shuffle(cards) for i in range(24): cards[i].correct = True print_cards() for i in range(24): cards[i].correct = False ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if cards[i].correct: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h + h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### return not ((num1 == num2) or (not 0 <= num1 <= 23) or (not 0 <= num2 <= 23) or cards[num1].correct or cards[num2].correct) def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### global correct_cnt cards[num1].correct = True cards[num2].correct = True print_cards() if cards[num1] == cards[num2]: correct_cnt += 2 return True cards[num1].correct = False cards[num2].correct = False print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while correct_cnt < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if 11 <= tries <= 13 or not (1 <= (tries % 10) <= 3): print(str(tries) + "th try. You got " + str(correct_cnt//2) + " pairs.") elif tries % 10 == 1: print(str(tries) + "st try. You got " + str(correct_cnt//2) + " pairs.") elif tries % 10 == 2: print(str(tries) + "nd try. You got " + str(correct_cnt//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(correct_cnt//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
21
33
variable
The cards list is a list containing a class called card, and the class card consists of an Image, a name, and a correct variable indicating whether the answer was correct.
cs1qa
null
null
null
null
null
Question: What elements is the cards list in? Code: from cs1graphics import * import time class Card(object): def __init__(self, img, name): self.img = img self.name = name self.correct = False def __eq__(self, c1): return self.name == c1.name def __ne__(self, c1): return not (self.name == c1.name) canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_cnt = 0 def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(Card(img, names[i])) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list import random random.shuffle(cards) for i in range(24): cards[i].correct = True print_cards() for i in range(24): cards[i].correct = False ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if cards[i].correct: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].img.moveTo(i_w + w, i_h + h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### return not ((num1 == num2) or (not 0 <= num1 <= 23) or (not 0 <= num2 <= 23) or cards[num1].correct or cards[num2].correct) def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### global correct_cnt cards[num1].correct = True cards[num2].correct = True print_cards() if cards[num1] == cards[num2]: correct_cnt += 2 return True cards[num1].correct = False cards[num2].correct = False print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while correct_cnt < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs if 11 <= tries <= 13 or not (1 <= (tries % 10) <= 3): print(str(tries) + "th try. You got " + str(correct_cnt//2) + " pairs.") elif tries % 10 == 1: print(str(tries) + "st try. You got " + str(correct_cnt//2) + " pairs.") elif tries % 10 == 2: print(str(tries) + "nd try. You got " + str(correct_cnt//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(correct_cnt//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
1
4
TA
Why did you make an exception after making two repetitions?
from cs1robots import * load_world("worlds/harvest2.wld") hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def harvest_up(): for i in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.pick_beeper() def turn1(): turn_right() hubo.move() turn_right() hubo.move() def turn2(): for i in range(2): hubo.move() hubo.turn_left() hubo.turn_left() for i in range(6): hubo.move() for i in range(2): harvest_up() turn1() harvest_down() turn2() harvest_up() turn1() harvest_down()
38
46
reasoning
Since the process of changing the direction was specified as a function, an unnecessary change of direction occurred at the end when it was repeated 3 times, so after repeating 2 times to subtract the part, an exception without trun2 was made separately.
cs1qa
null
null
null
null
null
Question: Why did you make an exception after making two repetitions? Code: from cs1robots import * load_world("worlds/harvest2.wld") hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def harvest_up(): for i in range(5): hubo.pick_beeper() turn_right() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.pick_beeper() def turn1(): turn_right() hubo.move() turn_right() hubo.move() def turn2(): for i in range(2): hubo.move() hubo.turn_left() hubo.turn_left() for i in range(6): hubo.move() for i in range(2): harvest_up() turn1() harvest_down() turn2() harvest_up() turn1() harvest_down()
1
2
TA
Can you explain the climb_up(n) function??
import cs1robots as cr cr.load_world("worlds/newspaper.wld") hubo=cr.Robot(beepers=1) def turn_right(): for i in range(3): hubo.turn_left() def climb_up(n): for i in range(n): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() def climb_down(n): for i in range(n): hubo.move() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() climb_up(4) hubo.drop_beeper() hubo.turn_left() hubo.turn_left() climb_down(4) hubo.move()
9
15
variable
The climb_up(n) function implies that the robot climbs the stairs n times. When I was right in front of the chin on the stairs
cs1qa
null
null
null
null
null
Question: Can you explain the climb_up(n) function?? Code: import cs1robots as cr cr.load_world("worlds/newspaper.wld") hubo=cr.Robot(beepers=1) def turn_right(): for i in range(3): hubo.turn_left() def climb_up(n): for i in range(n): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() def climb_down(n): for i in range(n): hubo.move() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() climb_up(4) hubo.drop_beeper() hubo.turn_left() hubo.turn_left() climb_down(4) hubo.move()
4
4
TA
What does -1 in line 8 mean?
import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40*sin(x)+40)-1 # Change this line to print out sine curve correctly. output_str = ' ' * character_count_per_line print (output_str+'*')
7
7
variable
In the case of Task 4 in the task description video, -1 is for task 4, so only the end of the task should be spaced * so I did -1 to process the spaces except for the last one.
cs1qa
null
null
null
null
null
Question: What does -1 in line 8 mean? Code: import math sin = math.sin pi = math.pi for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = int(40*sin(x)+40)-1 # Change this line to print out sine curve correctly. output_str = ' ' * character_count_per_line print (output_str+'*')
1
2
TA
Why can I delete line 17 from Task 3?
from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def jump_one_hurdle(): if not my_robot.front_is_clear(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
15
28
reasoning
This is because else executed when the if condition in line 28 is false.
cs1qa
null
null
null
null
null
Question: Why can I delete line 17 from Task 3? Code: from cs1robots import * # Your code should work with any of the world files below. # TIP: Press Ctrl + '/' (or Cmd + '/' if you are using a Mac) # to comment out or restore the whole line of the code in the editor. # load_world('worlds/hurdles1.wld') load_world('worlds/hurdles2.wld') # load_world('worlds/hurdles3.wld') my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def jump_one_hurdle(): if not my_robot.front_is_clear(): my_robot.turn_left() my_robot.move() turn_right() my_robot.move() turn_right() my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): if my_robot.front_is_clear(): my_robot.move() else: jump_one_hurdle()
1
3
TA
Could you briefly explain what the one_cycle function of tasks 3 and 4 does?
from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace("blue") def turn_right() : for i in range(3) : hubo.turn_left() def move_and_pick_5times() : for i in range(5) : move_and_pick() def move_and_pick() : hubo.move() hubo.pick_beeper() def one_cycle() : hubo.turn_left() hubo.pick_beeper() move_and_pick_5times() turn_right() move_and_pick() turn_right() move_and_pick_5times() hubo.turn_left() for i in range(3) : hubo.move() one_cycle()
4
21
variable
In task 4, it goes back and forth in the letter ㄹ, but once in the form of c is one cycle
cs1qa
null
null
null
null
null
Question: Could you briefly explain what the one_cycle function of tasks 3 and 4 does? Code: from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.set_trace("blue") def turn_right() : for i in range(3) : hubo.turn_left() def move_and_pick_5times() : for i in range(5) : move_and_pick() def move_and_pick() : hubo.move() hubo.pick_beeper() def one_cycle() : hubo.turn_left() hubo.pick_beeper() move_and_pick_5times() turn_right() move_and_pick() turn_right() move_and_pick_5times() hubo.turn_left() for i in range(3) : hubo.move() one_cycle()
4
1
TA
Why should we apply float() after receiving input() in Task 2?
a =float(input('Side a: ')) print('Value of a is', a) b = float(input('Side b: ')) print('Value of b is', b) c = float(input('Side c: ')) print('Value of c is', c) def is_triangle(a, b, c): if a<b: if b<c: l=c m=b s=a else: l=b m=a s=c else: if a<c: l=c m=a s=c else: l=a m=b s=c if l<m+s: return True else: return False if is_triangle(a,b,c): print('YES') else: print('NO')
0
32
reasoning
Just input() recognizes it as a string, so you have to add float() to recognize it as a number!
cs1qa
null
null
null
null
null
Question: Why should we apply float() after receiving input() in Task 2? Code: a =float(input('Side a: ')) print('Value of a is', a) b = float(input('Side b: ')) print('Value of b is', b) c = float(input('Side c: ')) print('Value of c is', c) def is_triangle(a, b, c): if a<b: if b<c: l=c m=b s=a else: l=b m=a s=c else: if a<c: l=c m=a s=c else: l=a m=b s=c if l<m+s: return True else: return False if is_triangle(a,b,c): print('YES') else: print('NO')
2
1
student
I am not entirely sure about my definition of plant
import time from cs1robots import * load_world("worlds/harvest3.wld") hubo = Robot(beepers=5) time.sleep(0.5) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def plant(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.drop_beeper() else: hubo.drop_beeper() def move_forward(): for i in range (5): plant() def move_one(): hubo.turn_left() plant() hubo.turn_left() move_forward() turn_right() plant() turn_right() move_forward() def move_two(): for i in range(2): move_one() plant() move_forward() move_two() hubo.turn_left() plant() hubo.turn_left() move_forward() plant()
9
15
code_understanding
You have the right idea for "plant"
cs1qa
null
null
null
null
null
Question: I am not entirely sure about my definition of plant Code: import time from cs1robots import * load_world("worlds/harvest3.wld") hubo = Robot(beepers=5) time.sleep(0.5) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def plant(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.drop_beeper() else: hubo.drop_beeper() def move_forward(): for i in range (5): plant() def move_one(): hubo.turn_left() plant() hubo.turn_left() move_forward() turn_right() plant() turn_right() move_forward() def move_two(): for i in range(2): move_one() plant() move_forward() move_two() hubo.turn_left() plant() hubo.turn_left() move_forward() plant()
1
4
TA
Can you briefly explain the function of each function?
from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace('blue') def harvest(): hubo.move() hubo.pick_beeper() def turn_right(): for i in range(3): hubo.turn_left() def back(): for i in range(2): hubo.turn_left() def harvest_right_up(): hubo.move() turn_right() harvest() hubo.turn_left() def harvest_left_up(): hubo.move() hubo.turn_left() harvest() turn_right() def harvest_left_down(): hubo.move() hubo.turn_left() harvest() turn_right() def harvest_right_down(): hubo.move() turn_right() harvest() hubo.turn_left() for i in range(5): hubo.move() hubo.turn_left() harvest() for i in range(5): harvest_right_up() for i in range(5): harvest_left_up() hubo.turn_left() for i in range(5): harvest_left_down() back() for i in range(4): harvest_right_down() hubo.turn_left() for i in range(4): harvest_right_up() for i in range(3): harvest_left_up() hubo.turn_left() for i in range(3): harvest_left_down() back() for i in range(2): harvest_right_down() hubo.turn_left() for i in range(2): harvest_right_up() harvest_left_up() hubo.turn_left() harvest_left_down()
6
40
variable
First, after moving to the bottom point, each function is defined. They are functions that pick beeper while going to the top right, bottom right, top left, and bottom left according to the moving direction.
cs1qa
null
null
null
null
null
Question: Can you briefly explain the function of each function? Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace('blue') def harvest(): hubo.move() hubo.pick_beeper() def turn_right(): for i in range(3): hubo.turn_left() def back(): for i in range(2): hubo.turn_left() def harvest_right_up(): hubo.move() turn_right() harvest() hubo.turn_left() def harvest_left_up(): hubo.move() hubo.turn_left() harvest() turn_right() def harvest_left_down(): hubo.move() hubo.turn_left() harvest() turn_right() def harvest_right_down(): hubo.move() turn_right() harvest() hubo.turn_left() for i in range(5): hubo.move() hubo.turn_left() harvest() for i in range(5): harvest_right_up() for i in range(5): harvest_left_up() hubo.turn_left() for i in range(5): harvest_left_down() back() for i in range(4): harvest_right_down() hubo.turn_left() for i in range(4): harvest_right_up() for i in range(3): harvest_left_up() hubo.turn_left() for i in range(3): harvest_left_down() back() for i in range(2): harvest_right_down() hubo.turn_left() for i in range(2): harvest_right_up() harvest_left_up() hubo.turn_left() harvest_left_down()
4
3
TA
Could you briefly explain why you set character_count_per_line to int(sin(x)*40+40) in Task 4?
import math sin = math.sin pi = math.pi for i in range(41): x = float(i) / 40.0 * 2 * pi if sin(x)>0: character_count_per_line = int(sin(x)*40+40) elif sin(x)<0: character_count_per_line = int(40+sin(x)*40) elif sin(x)==0: character_count_per_line = 40 output_str = '#' * character_count_per_line print(output_str)
8
8
reasoning
In the expected result, the maximum number of points is 80 and the minimum number of points is 0. If the function form is to be sin form I thought that the sin function was increased by 40 times and moved 40 spaces in the y-axis direction.
cs1qa
null
null
null
null
null
Question: Could you briefly explain why you set character_count_per_line to int(sin(x)*40+40) in Task 4? Code: import math sin = math.sin pi = math.pi for i in range(41): x = float(i) / 40.0 * 2 * pi if sin(x)>0: character_count_per_line = int(sin(x)*40+40) elif sin(x)<0: character_count_per_line = int(40+sin(x)*40) elif sin(x)==0: character_count_per_line = 40 output_str = '#' * character_count_per_line print(output_str)
9
1
TA
What information does an object of class Card in task2 have?
import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card: def __init__(self, suit, face, state=True): self.suit=suit self.face=face for i in range(13): if self.face==face_names[i]: self.value=value[i] break self.state=state def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck=[] for suit in suit_names: for face in face_names: deck.append(Card(suit, face)) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ total=0 for card in hand: total=total+card.value return total def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ article="a " if card.face in [8, "Ace"]: article="an " return (article+str(card.face)+" of "+str(card.suit)) def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ answer=str(input(prompt)) if answer=="y": return True elif answer=="n": return False else: print("I beg your pardon!") ask_yesno(promt) def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for i in range(len(dealer)): if dealer[i].state: imagename=str(dealer[i].suit)+"_"+str(dealer[i].face)+".png" img=Image(img_path+imagename) dealer[i].img=img else: img=Image(img_path+"Back.png") dealer[i].img=img for i in range(len(player)): if player[i].state: imagename=str(player[i].suit)+"_"+str(player[i].face)+".png" img=Image(img_path+imagename) player[i].img=img else: img=Image(img_path+"Back.png") player[i].img=img bj_board.clear() for i in range(len(dealer)): dealer[i].img.moveTo(x0, y0) dealer[i].img.setDepth(depth) bj_board.add(dealer[i].img) x0=x0+20 depth=depth-10 for i in range(len(player)): player[i].img.moveTo(x1, y1) player[i].img.setDepth(depth) bj_board.add(player[i].img) x1=x1+20 depth=depth-10 sum_dealer=hand_value(dealer) sum_player=hand_value(player) text_dealer="The dealer's Total : "+str(sum_dealer) text_player="The player's Total : "+str(sum_player) text1=Text(text_dealer, 20, Point(400, 100)) if dealer[0].state: bj_board.add(text1) text2=Text(text_player, 20, Point(400, 300)) bj_board.add(text2) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
17
26
variable
It is suit, face, value, image and state (determining whether it is hidden or not).
cs1qa
null
null
null
null
null
Question: What information does an object of class Card in task2 have? Code: import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card: def __init__(self, suit, face, state=True): self.suit=suit self.face=face for i in range(13): if self.face==face_names[i]: self.value=value[i] break self.state=state def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck=[] for suit in suit_names: for face in face_names: deck.append(Card(suit, face)) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ total=0 for card in hand: total=total+card.value return total def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ article="a " if card.face in [8, "Ace"]: article="an " return (article+str(card.face)+" of "+str(card.suit)) def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ answer=str(input(prompt)) if answer=="y": return True elif answer=="n": return False else: print("I beg your pardon!") ask_yesno(promt) def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 for i in range(len(dealer)): if dealer[i].state: imagename=str(dealer[i].suit)+"_"+str(dealer[i].face)+".png" img=Image(img_path+imagename) dealer[i].img=img else: img=Image(img_path+"Back.png") dealer[i].img=img for i in range(len(player)): if player[i].state: imagename=str(player[i].suit)+"_"+str(player[i].face)+".png" img=Image(img_path+imagename) player[i].img=img else: img=Image(img_path+"Back.png") player[i].img=img bj_board.clear() for i in range(len(dealer)): dealer[i].img.moveTo(x0, y0) dealer[i].img.setDepth(depth) bj_board.add(dealer[i].img) x0=x0+20 depth=depth-10 for i in range(len(player)): player[i].img.moveTo(x1, y1) player[i].img.setDepth(depth) bj_board.add(player[i].img) x1=x1+20 depth=depth-10 sum_dealer=hand_value(dealer) sum_player=hand_value(player) text_dealer="The dealer's Total : "+str(sum_dealer) text_player="The player's Total : "+str(sum_player) text1=Text(text_dealer, 20, Point(400, 100)) if dealer[0].state: bj_board.add(text1) text2=Text(text_player, 20, Point(400, 300)) bj_board.add(text2) def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
6
2
student
``` print_cards() ``` Is this the code you are visualizing?
from cs1graphics import * import time canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [Dohoo, Jeongmin, Jinyeong, Minsuk, Sanjae, Sungeun] num_pads = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i%2 == 0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 and num2 in correct_list: elif num1 == num2: elif not num1, num2 in num_pads: return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() if num1 == num2: correct_list.append(num1, num2) else: print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
null
null
code_understanding
Yes, it is
cs1qa
null
null
null
null
null
Question: ``` print_cards() ``` Is this the code you are visualizing? Code: from cs1graphics import * import time canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [Dohoo, Jeongmin, Jinyeong, Minsuk, Sanjae, Sungeun] num_pads = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i%2 == 0: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if num1 and num2 in correct_list: elif num1 == num2: elif not num1, num2 in num_pads: return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### print_cards() if num1 == num2: correct_list.append(num1, num2) else: print_cards() return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while True: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
5
0
TA
Can you explain the function of the global keyword in Task 1?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance global balance balance = balance + money ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance if balance >= money: balance = balance - money else: print("But you only have",balance,"won") ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output balance = 0 while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process == '': break elif process == 'd': money_deposit = int(input("How much do you want to deposit? ")) print("You deposited",money_deposit,"won") deposit(money_deposit) elif process == 'w': money_withdraw = int(input("how much do you want to withdraw? ")) print("You've withdraw",money_withdraw,"won") withdrawal(money_withdraw) elif process == 'c': print("Your current balance is", balance,"won") else: print("Please, press d or w or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
0
31
variable
In the framework of the problem, the balance is defined outside. In order to modify the variable inside the function, it is not a local variable, so you need to modify the global variable using global So I used the global keyword to modify the global variable defined outside as a local variable in the inner function.
cs1qa
null
null
null
null
null
Question: Can you explain the function of the global keyword in Task 1? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance global balance balance = balance + money ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance if balance >= money: balance = balance - money else: print("But you only have",balance,"won") ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output balance = 0 while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process == '': break elif process == 'd': money_deposit = int(input("How much do you want to deposit? ")) print("You deposited",money_deposit,"won") deposit(money_deposit) elif process == 'w': money_withdraw = int(input("how much do you want to withdraw? ")) print("You've withdraw",money_withdraw,"won") withdrawal(money_withdraw) elif process == 'c': print("Your current balance is", balance,"won") else: print("Please, press d or w or return") # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
2
1
TA
Please explain why you put the turn function in task2
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=20) hubo.set_trace('blue') def course(): for i in range(10): if hubo.front_is_clear(): if not hubo.on_beeper(): hubo.drop_beeper() hubo.move() else: if hubo.on_beeper(): hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() else: hubo.drop_beeper() hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def turn(): if not hubo.on_beeper(): hubo.drop_beeper() turn_right() hubo.move() turn_right() hubo.move() for i in range(2): course() turn() course() if not hubo.on_beeper(): hubo.drop_beeper()
34
39
reasoning
The reason for using the turn() function is Going in a straight line and then coming back as a unit There is no case of going up at the end, so I used a turn
cs1qa
null
null
null
null
null
Question: Please explain why you put the turn function in task2 Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot(beepers=20) hubo.set_trace('blue') def course(): for i in range(10): if hubo.front_is_clear(): if not hubo.on_beeper(): hubo.drop_beeper() hubo.move() else: if hubo.on_beeper(): hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() else: hubo.drop_beeper() hubo.turn_left() hubo.move() if not hubo.on_beeper(): hubo.drop_beeper() hubo.turn_left() hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def turn(): if not hubo.on_beeper(): hubo.drop_beeper() turn_right() hubo.move() turn_right() hubo.move() for i in range(2): course() turn() course() if not hubo.on_beeper(): hubo.drop_beeper()
9
0
TA
Can you explain reverse_cards first?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 check_num=0 class Card(object): def personality(self, img, name): self.img=img self.name=name def state(self): self.state=False def initialize(): for i in range(6): for k in range(4): img = Image(path+names[i]) temp=Card() temp.personality(img, names[i]) temp.state() cards.append(temp) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) def reverse_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(4) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if cards[i].state==True: cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(2) def is_valid(num1, num2): if num1<0 or num1>23 or num2<0 or num2>23: return False elif num1==num2: return False elif cards[num1].state==True or cards[num2].state==True: return False else: return True def check(num1, num2): cards[num1].state=True cards[num2].state=True print_cards() if cards[num1].name==cards[num2].name: return True else: cards[num1].state=False cards[num2].state=False return False initialize() reverse_cards() print_cards() print("### Welcome to the Python Memento game!!! ###") while not check_num==12: print(str(tries) + "th try. You got " + str(check_num) + " pairs.") num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") check_num=check_num+1 print_cards() else: print("Wrong!") print_cards() tries=tries+1
45
58
variable
reverse_cards is a function that shows cards shuffled randomly over the first 4 seconds.
cs1qa
null
null
null
null
null
Question: Can you explain reverse_cards first? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 check_num=0 class Card(object): def personality(self, img, name): self.img=img self.name=name def state(self): self.state=False def initialize(): for i in range(6): for k in range(4): img = Image(path+names[i]) temp=Card() temp.personality(img, names[i]) temp.state() cards.append(temp) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) random.shuffle(cards) def reverse_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(4) def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if cards[i].state==True: cards[i].img.moveTo(i_w + w, i_h+h) canvas.add(cards[i].img) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(2) def is_valid(num1, num2): if num1<0 or num1>23 or num2<0 or num2>23: return False elif num1==num2: return False elif cards[num1].state==True or cards[num2].state==True: return False else: return True def check(num1, num2): cards[num1].state=True cards[num2].state=True print_cards() if cards[num1].name==cards[num2].name: return True else: cards[num1].state=False cards[num2].state=False return False initialize() reverse_cards() print_cards() print("### Welcome to the Python Memento game!!! ###") while not check_num==12: print(str(tries) + "th try. You got " + str(check_num) + " pairs.") num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") check_num=check_num+1 print_cards() else: print("Wrong!") print_cards() tries=tries+1
2
3
TA
What is task4's one_cycle function?
from cs1robots import * load_world('worlds/harvest4.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def one_length(): for i in range(5): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def one_cycle(): one_length() hubo.turn_left() hubo.move() hubo.turn_left() one_length() hubo.move() for i in range(2): one_cycle() turn_right() hubo.move() turn_right() one_cycle()
18
23
variable
This is a function that makes it (back and forth) twice horizontally
cs1qa
null
null
null
null
null
Question: What is task4's one_cycle function? Code: from cs1robots import * load_world('worlds/harvest4.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def one_length(): for i in range(5): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() def one_cycle(): one_length() hubo.turn_left() hubo.move() hubo.turn_left() one_length() hubo.move() for i in range(2): one_cycle() turn_right() hubo.move() turn_right() one_cycle()
5
0
TA
How did you use the balance variable in Task1?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance+=money print('You deposited', money, 'won') # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance if balance>=money: balance-=money print('You have withdraw', money, 'won') else: print('You have withdrawn', money, 'won') print('But you only have', balance, 'won') # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)?? ") if process=='c': print('Your current balance is', balance, 'won') elif process=='d': money=int(input('How much do you want to deposit?')) deposit(money) elif process=='w': money=int(input('How much do you want to withdraw?')) withdrawal(money) elif process=='': return else: print('Please, press d or w or c or none to quit') # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
0
34
variable
balance was considered and resolved as a balance
cs1qa
null
null
null
null
null
Question: How did you use the balance variable in Task1? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance+=money print('You deposited', money, 'won') # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance if balance>=money: balance-=money print('You have withdraw', money, 'won') else: print('You have withdrawn', money, 'won') print('But you only have', balance, 'won') # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)?? ") if process=='c': print('Your current balance is', balance, 'won') elif process=='d': money=int(input('How much do you want to deposit?')) deposit(money) elif process=='w': money=int(input('How much do you want to withdraw?')) withdrawal(money) elif process=='': return else: print('Please, press d or w or c or none to quit') # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
6
1
TA
in task2 How did you implement count_integers?
import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ l=[] for i in range(trials): l.append(random.randint(lb,ub)) return l pass def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ a=0 for i in range(len(num_list)): a=a+num_list[i] a=a/len(num_list) return a pass def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ l=[] a=max(num_list)-min(num_list) for i in range(a+1): x=min(num_list)+i l.append((x,num_list.count(x))) return l pass # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
32
45
variable
After finding out the range of numbers in the list through max and min, I used the count function to check how many numbers correspond from min.
cs1qa
null
null
null
null
null
Question: in task2 How did you implement count_integers? Code: import random def drawing_integers(lb, ub, trials): """ Make a list of the integers :param lb: the lower bound of the integers :param ub: the upper bound of the integers :param trials: the number of trials :return: an integers list. Ex) [1, 4, 3, 5, 2] """ l=[] for i in range(trials): l.append(random.randint(lb,ub)) return l pass def average_integers(num_list): """ Compute the average of the integers in the num_list :param num_list: input list :return: average value of the list """ a=0 for i in range(len(num_list)): a=a+num_list[i] a=a/len(num_list) return a pass def count_integers(num_list): """ Count the integers in the num_list :param num_list: input list :return: A list of tuples that consist of the integer and its frequency """ l=[] a=max(num_list)-min(num_list) for i in range(a+1): x=min(num_list)+i l.append((x,num_list.count(x))) return l pass # Run the program list1 = drawing_integers(1, 6, 20) print(list1) print(average_integers(list1)) print(count_integers(list1)) print() list2 = drawing_integers(5, 12, 15) print(list2) print(average_integers(list2)) print(count_integers(list2))
1
1
TA
In Task 2 while my_robot.on_beeper(): my_robot.pick_beeper() ``` while my_robot.on_beeper(): my_robot.pick_beeper() ``` You used the while statement twice. Is there a reason...?
from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) #load_world( "worlds/trash2.wld" ) my_robot = Robot() my_robot.set_trace('red') def turn_right(): for i in range(3): my_robot.turn_left() def turn(): for i in range(2): my_robot.turn_left() def go(): while my_robot.front_is_clear(): while my_robot.on_beeper(): my_robot.pick_beeper() my_robot.move() while my_robot.on_beeper(): my_robot.pick_beeper() turn() while my_robot.front_is_clear(): my_robot.move() turn_right() my_robot.move() while my_robot.carries_beepers(): my_robot.drop_beeper() turn() my_robot.move() my_robot.turn_left() go()
15
31
reasoning
It seems to be to solve the last 10 beeper.
cs1qa
null
null
null
null
null
Question: In Task 2 while my_robot.on_beeper(): my_robot.pick_beeper() ``` while my_robot.on_beeper(): my_robot.pick_beeper() ``` You used the while statement twice. Is there a reason...? Code: from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) #load_world( "worlds/trash2.wld" ) my_robot = Robot() my_robot.set_trace('red') def turn_right(): for i in range(3): my_robot.turn_left() def turn(): for i in range(2): my_robot.turn_left() def go(): while my_robot.front_is_clear(): while my_robot.on_beeper(): my_robot.pick_beeper() my_robot.move() while my_robot.on_beeper(): my_robot.pick_beeper() turn() while my_robot.front_is_clear(): my_robot.move() turn_right() my_robot.move() while my_robot.carries_beepers(): my_robot.drop_beeper() turn() my_robot.move() my_robot.turn_left() go()
8
0
TA
What is the f.read() function in task1?
from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # ... pass f1 = open(output_filename, 'w') n = len(input_filenames) for i in range(n): f = open(input_filenames[i], 'r') data = f.read() f1.write(data) f.close() f1.close() merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
12
12
variable
The f.read function returns the entire file as a string.
cs1qa
null
null
null
null
null
Question: What is the f.read() function in task1? Code: from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # ... pass f1 = open(output_filename, 'w') n = len(input_filenames) for i in range(n): f = open(input_filenames[i], 'r') data = f.read() f1.write(data) f.close() f1.close() merge(['kaist1.txt', 'kaist2.txt', 'kaist3.txt'], 'output.txt') sleep(0.5) # Wait 0.5 seconds before creating a download link. elice_utils.send_file('output.txt')
3
4
TA
What is the task 5 threshold setting criteria?
from cs1media import * # This code converts an image into a black & white poster. threshold = 85 image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness <= threshold: image.set(x, y, (0,0,255)) elif average_brightness <= 2*threshold: image.set(x, y, (0,255,0)) else: image.set(x, y, (255,255,0)) image.show()
13
18
code_explain
Oh, I don't know if it's correct, but I set it to one third
cs1qa
null
null
null
null
null
Question: What is the task 5 threshold setting criteria? Code: from cs1media import * # This code converts an image into a black & white poster. threshold = 85 image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness <= threshold: image.set(x, y, (0,0,255)) elif average_brightness <= 2*threshold: image.set(x, y, (0,255,0)) else: image.set(x, y, (255,255,0)) image.show()
2
4
TA
Can you tell me why you used this condition while left_not_clear in task 5?
from cs1robots import * # Your code must work for empty worlds of all possible sizes. # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... create_world(avenues=11, streets=10) my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def turn_around1(): turn_right() if my_robot.front_is_clear(): my_robot.move() turn_right() def turn_around2(): my_robot.turn_left() if my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_around1() while my_robot.left_is_clear(): while my_robot.front_is_clear(): my_robot.move() turn_around2() while my_robot.front_is_clear(): my_robot.move() turn_around1() while my_robot.front_is_clear(): my_robot.move()
null
null
reasoning
At first, I only thought about the even number sequence, so at the end it comes down from top to bottom, so I used it to end the loop before that.
cs1qa
null
null
null
null
null
Question: Can you tell me why you used this condition while left_not_clear in task 5? Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) # ... create_world(avenues=11, streets=10) my_robot = Robot() my_robot.set_trace('blue') def turn_right(): for i in range(3): my_robot.turn_left() def turn_around1(): turn_right() if my_robot.front_is_clear(): my_robot.move() turn_right() def turn_around2(): my_robot.turn_left() if my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_around1() while my_robot.left_is_clear(): while my_robot.front_is_clear(): my_robot.move() turn_around2() while my_robot.front_is_clear(): my_robot.move() turn_around1() while my_robot.front_is_clear(): my_robot.move()
8
1
TA
How did you process the data in #2?
f = open("average-latitude-longitude-countries.csv", "r") name = [] coordinate = [] a = [] for line in f: a.append(line.split(",")) for i in range(1,len(a)): if len(a[i]) == 4: name.append((a[i][0][1:-1], a[i][1][1:-1])) coordinate.append((a[i][0][1:-1],(float(a[i][2]), float(a[i][3])))) else: name.append((a[i][0][1:-1], a[i][1][1:] + "," + a[i][2][:-1])) coordinate.append((a[i][0][1:-1], (float(a[i][3]), float(a[i][4])))) for i in range(len(coordinate)): if coordinate[i][1][0] < 0: print(name[i][1]) print(name) print(coordinate) b = str(input("Enter country code: ")) for i in range(len(name)): if b == name[i][0]: print(name[i][1])
4
12
code_explain
First, the data was divided by the split function based on',', and the front and back "" were cut and saved in the list. In addition, countries containing, were classified separately and processed.
cs1qa
null
null
null
null
null
Question: How did you process the data in #2? Code: f = open("average-latitude-longitude-countries.csv", "r") name = [] coordinate = [] a = [] for line in f: a.append(line.split(",")) for i in range(1,len(a)): if len(a[i]) == 4: name.append((a[i][0][1:-1], a[i][1][1:-1])) coordinate.append((a[i][0][1:-1],(float(a[i][2]), float(a[i][3])))) else: name.append((a[i][0][1:-1], a[i][1][1:] + "," + a[i][2][:-1])) coordinate.append((a[i][0][1:-1], (float(a[i][3]), float(a[i][4])))) for i in range(len(coordinate)): if coordinate[i][1][0] < 0: print(name[i][1]) print(name) print(coordinate) b = str(input("Enter country code: ")) for i in range(len(name)): if b == name[i][0]: print(name[i][1])
3
4
TA
How did you do the color classification in task5, and where is the code that makes it blue when dark?
from cs1media import * # This code converts an image into a black & white poster. white = (255, 255, 255) black = (0, 0, 0) yellow = (255, 255, 0) blue = (0, 0, 255) green = (0, 255, 0) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > 150: image.set(x, y, yellow) elif average_brightness < 70: image.set(x, y, blue) else: image.set(x, y, green) image.show()
17
22
code_explain
The average_brightness value is divided by 150 and 70, and lines 20 and 21 are blue.
cs1qa
null
null
null
null
null
Question: How did you do the color classification in task5, and where is the code that makes it blue when dark? Code: from cs1media import * # This code converts an image into a black & white poster. white = (255, 255, 255) black = (0, 0, 0) yellow = (255, 255, 0) blue = (0, 0, 255) green = (0, 255, 0) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > 150: image.set(x, y, yellow) elif average_brightness < 70: image.set(x, y, blue) else: image.set(x, y, green) image.show()
3
0
TA
Please explain what the harvest_more() function does and does!
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def check_beeper(): if hubo.on_beeper(): hubo.pick_beeper() # Scans 2 floors(rows) which have {num} columns def scan_two_floor(num): for k in range(num - 1): check_beeper() hubo.move() check_beeper() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): check_beeper() hubo.move() check_beeper() # Input the number of row and column of check area,{row},{col} each def harvest_more(row, col): itr = int(row / 2) for k in range(itr): scan_two_floor(col) if k != itr -1: turn_right() hubo.move() turn_right() if row % 2 == 1: # If number of the row is odd turn_right() hubo.move() turn_right() for k in range(col - 1): check_beeper() hubo.move() check_beeper() hubo.move() harvest_more(6,6)
29
44
variable
This is the code that harvests the area by inserting the row and col values of the area to be harvested.
cs1qa
null
null
null
null
null
Question: Please explain what the harvest_more() function does and does! Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def check_beeper(): if hubo.on_beeper(): hubo.pick_beeper() # Scans 2 floors(rows) which have {num} columns def scan_two_floor(num): for k in range(num - 1): check_beeper() hubo.move() check_beeper() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): check_beeper() hubo.move() check_beeper() # Input the number of row and column of check area,{row},{col} each def harvest_more(row, col): itr = int(row / 2) for k in range(itr): scan_two_floor(col) if k != itr -1: turn_right() hubo.move() turn_right() if row % 2 == 1: # If number of the row is odd turn_right() hubo.move() turn_right() for k in range(col - 1): check_beeper() hubo.move() check_beeper() hubo.move() harvest_more(6,6)
9
1
TA
What does assert do in the Card class?
import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card(): def __init__(self, suit, face, value, image): self.suit = suit self.face = face self.value = value self.image = image self.state = True def string(self): article = "a " if self.face in [8, "Ace"]: article = "an " return article +str(self.face)+" of "+self.suit def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck = [] for i in range(4): for j in range(13): card = Card(suit_names[i], face_names[j], value[j], Image(img_path+suit_names[i]+'_'+face_names[j]+'.png')) deck.append(card) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ value = 0 for card in hand: value = value+card.value return value def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ return card.string() def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ while True: r = input(prompt) if r=='y': return True elif r=='n': return False else: print('I beg your pardon') def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for card in dealer: if card.state: image = card.image else: image = Image(img_path+'Back.png') image.moveTo(x0, y0) image.setDepth(depth) bj_board.add(image) x0 += 15 depth -=1 for card in player: if card.state: image = card.image else: image = Image(img_path+'Back.png') image.moveTo(x1, y1) image.setDepth(depth) bj_board.add(image) depth -=1 x1 += 15 def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
null
null
variable
I haven't used it, but if you look at the video, it checks if the argument you put in the constructor is an appropriate card name.
cs1qa
null
null
null
null
null
Question: What does assert do in the Card class? Code: import random from cs1graphics import * img_path = './images/' suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] face_names = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King'] value = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] bj_board = Canvas(600, 400, 'dark green', 'Black Jack 101') """ Define the Card class """ class Card(): def __init__(self, suit, face, value, image): self.suit = suit self.face = face self.value = value self.image = image self.state = True def string(self): article = "a " if self.face in [8, "Ace"]: article = "an " return article +str(self.face)+" of "+self.suit def create_deck(number = 1): """ Create a list("deck") of all 52 cards, shuffle them and return the list. The list 'deck' have to include Card objects A Card is represented by a object with four attributes: the face, the suit, value, state, and the image object First, Have to define class 'Card' """ deck = [] for i in range(4): for j in range(13): card = Card(suit_names[i], face_names[j], value[j], Image(img_path+suit_names[i]+'_'+face_names[j]+'.png')) deck.append(card) random.shuffle(deck) return deck def hand_value(hand): """ hand is a list including card objects Compute the value of the cards in the list "hand" """ value = 0 for card in hand: value = value+card.value return value def card_string(card): """ Parameter "card" is a Card object Return a nice string to represent a card (sucn as "a King of Spades" or "an Ace of Diamonds") """ return card.string() def ask_yesno(prompt): """ Display the text prompt and let's the user enter a string. If the user enters "y", the function returns "True", and if the user enters "n", the function returns "False". If the user enters anything else, the function prints "I beg your pardon!", and asks again, repreting this until the user has entered a correct string. """ while True: r = input(prompt) if r=='y': return True elif r=='n': return False else: print('I beg your pardon') def draw_card(dealer,player): """ This funuction add the cards of dealer and player to canvas, bj_board. If the state of each Card object is false, then you have to show the hidden card image(Back.png). The dealer's first card is hidden state. The parameter dealer and player are List objects including Card Objects. The start position of dealer's card is (100,100). The start position of player's card is (100,300). You can use the following methods for positioning images and text: Image() Object, Text() Object, moveTo() method, setDepth() method. You should use help function - help('cs1graphics.Image') -> about Image(), moveTo(), setDepth() help('cs1graphics.Text') -> about Text(),moveTo(), setDepth() """ depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for card in dealer: if card.state: image = card.image else: image = Image(img_path+'Back.png') image.moveTo(x0, y0) image.setDepth(depth) bj_board.add(image) x0 += 15 depth -=1 for card in player: if card.state: image = card.image else: image = Image(img_path+'Back.png') image.moveTo(x1, y1) image.setDepth(depth) bj_board.add(image) depth -=1 x1 += 15 def main(): deck = [] while True: # prompt for starting a new game and create a deck print ("Welcome to Black Jack 101!\n") if len(deck) < 12: deck = create_deck() # create two hands of dealer and player dealer = [] player = [] # initial two dealings card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt a hidden card") card.state=False dealer.append(card) card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # player's turn to draw cards while hand_value(player) < 21 and ask_yesno("Would you like another card? (y/n) "): # draw a card for the player card = deck.pop() print ("You are dealt " + card_string(card)) player.append(card) print ("Your total is", hand_value(player)) draw_card(dealer,player) # if the player's score is over 21, the player loses immediately. if hand_value(player) > 21: print ("You went over 21! You lost.") dealer[0].state = True draw_card(dealer,player) else: # draw cards for the dealer while the dealer's score is less than 17 print ("\nThe dealer's hidden card was " + card_string(dealer[0])) while hand_value(dealer) < 17: card = deck.pop() print ("Dealer is dealt " + card_string(card)) dealer.append(card) print ("The dealer's total is", hand_value(dealer)) dealer[0].state = True draw_card(dealer,player) # summary player_total = hand_value(player) dealer_total = hand_value(dealer) print ("\nYour total is", player_total) print ("The dealer's total is", dealer_total) if dealer_total > 21: print ("The dealer went over 21! You win!") else: if player_total > dealer_total: print ("You win!") elif player_total < dealer_total: print ("You lost!") else: print ("You have a tie!") if not ask_yesno("\nPlay another round? (y/n) "): bj_board.close() break main()
10
0
TA
What is the identity of other objects other than mushrooms?
from cs1graphics import * from time import sleep _scene = None _world = None t = 0.2 def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) #blocks block = Rectangle(40, 40, Point(200, 100)) block.setFillColor('brown') qmark = Text("?", 20, Point(200, 100)) qmark.setFontColor('Yellow') qmark.setDepth(48) _scene.add(qmark) block2 = block.clone() block2.move(40, 0) block.setDepth(50) _scene.add(block) _scene.add(block2) #pipe pipe = Polygon(Point(400, 150), Point(400, 160), Point(410, 160), Point(410, 320), Point(470, 320), Point(470, 160), Point(480, 160), Point(480, 150)) pipe.setFillColor('lightgreen') pipe.setDepth(10) pipe.move(-10, 0) _scene.add(pipe) class Mushroom(object): def __init__(self, x=200, y=92): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(lowermush) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom _scene.add(self.layer) def diappear(self): self.layer.scale(0.001) def move(self, x, y): self.layer.move(x, y) def arise(self): self.layer.setDepth(45) self.layer.move(0, -20) COLOR = ['Red', 'Blue'] TYPE = ['super', 'normal'] class Mario(object): def __init__(self, color='Blue', type_='normal'): assert type_ in TYPE and color in COLOR self.color = color self.type = type_ self.step_size = 3 # Constructing Mario mario = Layer() # body body = Rectangle(33, 22, Point(200, 200)) body.setFillColor(color) body.setDepth(50) mario.add(body) # face face = Ellipse(30, 20, Point(200, 180)) face.setFillColor('beige') face.setDepth(40) mario.add(face) #hat hat = Polygon(Point(185, 175), Point(220, 175), Point(220, 173), Point(215, 173), Point(212, 168), Point(188, 168)) hat.setFillColor(color) hat.setDepth(39) mario.add(hat) #beard beard = Polygon(Point(207, 183), Point(217, 183), Point(215, 180), Point(209, 180)) beard.setFillColor('Brown') beard.setDepth(38) mario.add(beard) shoe = Layer() #left shoe lshoe = Rectangle(15, 6, Point(191, 215)) lshoe.setFillColor('black') lshoe.setDepth(52) shoe.add(lshoe) #right shoe rshoe = lshoe.clone() rshoe.move(17, 0) shoe.add(rshoe) mario.add(shoe) # save alias of moveable parts self.layer = mario self.body = body self.hat = hat self.shoe = shoe _scene.add(self.layer) self.moving_part_count = 0 if type_ == 'super': self.supermario() def shoe_move(self): if self.moving_part_count % 3 == 0: self.shoe.move(3, 0) elif self.moving_part_count % 3 == 1: self.shoe.move(-5,0) else: self.shoe.move(2,0) self.moving_part_count += 1 if self.moving_part_count % 3 == 0: self.moving_part_count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def supermario(self): tempPt = self.body.getReferencePoint() self.layer.adjustReference(tempPt.getX(), tempPt.getY()) for i in range(3): self.layer.scale(1.3) sleep(t/2) self.layer.scale(0.9) sleep(t/2) def walk(self,x=20): assert x > 0 total_step = int(x / self.step_size) for i in range(total_step): sleep(t/4) self.move(self.step_size, 0) self.shoe_move() def show_animation(): sleep(t) mario.move(0, -50) mushroom.arise() sleep(t) mario.move(0, 50) mushroom.move(0, 8) for i in range(7): sleep(t/2) mushroom.move(10, 0) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t/2) mushroom.move(0, 50) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t) mushroom.move(0, 50) sleep(t/2) mushroom.diappear() sleep(t/2) mario.supermario() for i in range(6): sleep(t/2) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() for i in range(2): sleep(t) mario.move(28, -60) for i in range(1): sleep(t) mario.move(32, 40) sleep(2*t) for i in range(4): sleep(t) mario.move(0, 25) def interactive_example(): while True: e = _scene.wait() d = e.getDescription() if d == "keyboard": k = e.getKey() if k == "q": _scene.close() break elif k == "w": mario.walk(20) elif k == "r": mario.walk(40) elif k == "j": mario.move(0, -50) sleep(t) mario.move(0, 50) create_world() mario = Mario('Blue', 'normal') mushroom = Mushroom(200, 92) # show_animation() interactive_example()
null
null
variable
I'm a friend called bike
cs1qa
null
null
null
null
null
Question: What is the identity of other objects other than mushrooms? Code: from cs1graphics import * from time import sleep _scene = None _world = None t = 0.2 def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ grass = Rectangle(1000, 150, Point(250, 250)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) #blocks block = Rectangle(40, 40, Point(200, 100)) block.setFillColor('brown') qmark = Text("?", 20, Point(200, 100)) qmark.setFontColor('Yellow') qmark.setDepth(48) _scene.add(qmark) block2 = block.clone() block2.move(40, 0) block.setDepth(50) _scene.add(block) _scene.add(block2) #pipe pipe = Polygon(Point(400, 150), Point(400, 160), Point(410, 160), Point(410, 320), Point(470, 320), Point(470, 160), Point(480, 160), Point(480, 150)) pipe.setFillColor('lightgreen') pipe.setDepth(10) pipe.move(-10, 0) _scene.add(pipe) class Mushroom(object): def __init__(self, x=200, y=92): mushroom = Layer() uppermush = Ellipse(38, 18, Point(x, y)) uppermush.setFillColor('red') uppermush.setDepth(52) lowermush = Ellipse(35, 25, Point(x, y+8)) lowermush.setFillColor('beige') lowermush.setDepth(53) mushroom.add(lowermush) mushroom.add(uppermush) mushroom.setDepth(52) self.layer = mushroom _scene.add(self.layer) def diappear(self): self.layer.scale(0.001) def move(self, x, y): self.layer.move(x, y) def arise(self): self.layer.setDepth(45) self.layer.move(0, -20) COLOR = ['Red', 'Blue'] TYPE = ['super', 'normal'] class Mario(object): def __init__(self, color='Blue', type_='normal'): assert type_ in TYPE and color in COLOR self.color = color self.type = type_ self.step_size = 3 # Constructing Mario mario = Layer() # body body = Rectangle(33, 22, Point(200, 200)) body.setFillColor(color) body.setDepth(50) mario.add(body) # face face = Ellipse(30, 20, Point(200, 180)) face.setFillColor('beige') face.setDepth(40) mario.add(face) #hat hat = Polygon(Point(185, 175), Point(220, 175), Point(220, 173), Point(215, 173), Point(212, 168), Point(188, 168)) hat.setFillColor(color) hat.setDepth(39) mario.add(hat) #beard beard = Polygon(Point(207, 183), Point(217, 183), Point(215, 180), Point(209, 180)) beard.setFillColor('Brown') beard.setDepth(38) mario.add(beard) shoe = Layer() #left shoe lshoe = Rectangle(15, 6, Point(191, 215)) lshoe.setFillColor('black') lshoe.setDepth(52) shoe.add(lshoe) #right shoe rshoe = lshoe.clone() rshoe.move(17, 0) shoe.add(rshoe) mario.add(shoe) # save alias of moveable parts self.layer = mario self.body = body self.hat = hat self.shoe = shoe _scene.add(self.layer) self.moving_part_count = 0 if type_ == 'super': self.supermario() def shoe_move(self): if self.moving_part_count % 3 == 0: self.shoe.move(3, 0) elif self.moving_part_count % 3 == 1: self.shoe.move(-5,0) else: self.shoe.move(2,0) self.moving_part_count += 1 if self.moving_part_count % 3 == 0: self.moving_part_count = 0 def move(self,x=10,y=0): self.layer.move(x,y) def supermario(self): tempPt = self.body.getReferencePoint() self.layer.adjustReference(tempPt.getX(), tempPt.getY()) for i in range(3): self.layer.scale(1.3) sleep(t/2) self.layer.scale(0.9) sleep(t/2) def walk(self,x=20): assert x > 0 total_step = int(x / self.step_size) for i in range(total_step): sleep(t/4) self.move(self.step_size, 0) self.shoe_move() def show_animation(): sleep(t) mario.move(0, -50) mushroom.arise() sleep(t) mario.move(0, 50) mushroom.move(0, 8) for i in range(7): sleep(t/2) mushroom.move(10, 0) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t/2) mushroom.move(0, 50) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() sleep(t) mushroom.move(0, 50) sleep(t/2) mushroom.diappear() sleep(t/2) mario.supermario() for i in range(6): sleep(t/2) mario.move(10, 0) mario.shoe_move() sleep(t/2) mario.shoe_move() for i in range(2): sleep(t) mario.move(28, -60) for i in range(1): sleep(t) mario.move(32, 40) sleep(2*t) for i in range(4): sleep(t) mario.move(0, 25) def interactive_example(): while True: e = _scene.wait() d = e.getDescription() if d == "keyboard": k = e.getKey() if k == "q": _scene.close() break elif k == "w": mario.walk(20) elif k == "r": mario.walk(40) elif k == "j": mario.move(0, -50) sleep(t) mario.move(0, 50) create_world() mario = Mario('Blue', 'normal') mushroom = Mushroom(200, 92) # show_animation() interactive_example()
1
3
TA
What is the reason for writing odd_line and even_line?
from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.move() hubo.pick_beeper() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def one_line_harvest(): for i in range(5): hubo.move() hubo.pick_beeper() def even_line_harvest(): one_line_harvest() turn_right() hubo.move() turn_right() hubo.pick_beeper() def odd_line_harvest(): one_line_harvest() hubo.turn_left() hubo.move() hubo.turn_left() hubo.pick_beeper() def two_lines_harvest(): even_line_harvest() odd_line_harvest() two_lines_harvest() two_lines_harvest() two_lines_harvest()
13
27
reasoning
In even-numbered lines, it moves one line and then turns to the right, but in odd-numbered lines, it moves one line and turns to the left, so I wrote a difference.
cs1qa
null
null
null
null
null
Question: What is the reason for writing odd_line and even_line? Code: from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.move() hubo.pick_beeper() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def one_line_harvest(): for i in range(5): hubo.move() hubo.pick_beeper() def even_line_harvest(): one_line_harvest() turn_right() hubo.move() turn_right() hubo.pick_beeper() def odd_line_harvest(): one_line_harvest() hubo.turn_left() hubo.move() hubo.turn_left() hubo.pick_beeper() def two_lines_harvest(): even_line_harvest() odd_line_harvest() two_lines_harvest() two_lines_harvest() two_lines_harvest()
6
2
TA
Please briefly explain the check function algorithm in Task3!
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if (num1 in correct_list) or (num2 in correct_list): return False if (num1 == num2): return False if (num1 > 23 or num1 < 0) or (num2>23 or num2<0): return False return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1 or i==num2: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] == cards[num2][1]: return True else: return False initialize() correct_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] print_cards() correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ########################################################################### tries += 1
76
105
code_explain
The check function of task3 places the cards in the same way as print cards, but is squeezed so that only the cards corresponding to num1 and num2 selected by the user are displayed, and returns true when the two cards are the same, and returns False when they are different.
cs1qa
null
null
null
null
null
Question: Please briefly explain the check function algorithm in Task3! Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 1 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if i in correct_list: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. ########################################################################### if (num1 in correct_list) or (num2 in correct_list): return False if (num1 == num2): return False if (num1 > 23 or num1 < 0) or (num2>23 or num2<0): return False return True def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): if i==num1 or i==num2: cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) if cards[num1][1] == cards[num2][1]: correct_list.append(num1) correct_list.append(num2) print_cards() if cards[num1][1] == cards[num2][1]: return True else: return False initialize() correct_list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23] print_cards() correct_list = [] print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list) < 24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ########################################################################### tries += 1
6
2
TA
Please explain the is_valid function in step 3
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if (correct_list.count(i)==1)or(tries==0): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range if (0<=num1)&(num1<=23)&(0<=num2)&(num2<=23)&(num1!=num2)&(correct_list.count(num1)==0)&(correct_list.count(num2)==0): return True # Return Boolean value according to the result. ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) if cards[num1][1]==cards[num2][1]: print_cards() return True print_cards() correct_list.pop() correct_list.pop() print_cards() return False initialize() print_cards() tries += 1 print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
62
73
variable
In the case of is_valid, if two numbers to be assigned are 0 or more and 23 or less, they are not equal to each other and are not in the count_list, True is returned.
cs1qa
null
null
null
null
null
Question: Please explain the is_valid function in step 3 Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if (correct_list.count(i)==1)or(tries==0): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range if (0<=num1)&(num1<=23)&(0<=num2)&(num2<=23)&(num1!=num2)&(correct_list.count(num1)==0)&(correct_list.count(num2)==0): return True # Return Boolean value according to the result. ########################################################################### return False def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. ########################################################################### correct_list.append(num1) correct_list.append(num2) if cards[num1][1]==cards[num2][1]: print_cards() return True print_cards() correct_list.pop() correct_list.pop() print_cards() return False initialize() print_cards() tries += 1 print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while len(correct_list)<24: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries += 1 ###########################################################################
3
4
TA
in task5 image.set(x, y, yellow) Mean?
from cs1media import * # This code converts an image into a black & white poster. threshold1 = 100 threshold2 = 200 white = (255, 255, 255) black = (0, 0, 0) yellow = (255, 255, 0) green = (0, 255, 0) blue = (0, 0, 255) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > threshold2: image.set(x, y, yellow) elif average_brightness < threshold1: image.set(x, y, blue) else: image.set(x, y, green) image.show()
20
20
variable
The pixel color of the x and y coordinates is set to yellow as defined above.
cs1qa
null
null
null
null
null
Question: in task5 image.set(x, y, yellow) Mean? Code: from cs1media import * # This code converts an image into a black & white poster. threshold1 = 100 threshold2 = 200 white = (255, 255, 255) black = (0, 0, 0) yellow = (255, 255, 0) green = (0, 255, 0) blue = (0, 0, 255) image = load_picture('./images/minion.jpg') width, height = image.size() for y in range(height): for x in range(width): r, g, b = image.get(x, y) average_brightness = (r + g + b) // 3 if average_brightness > threshold2: image.set(x, y, yellow) elif average_brightness < threshold1: image.set(x, y, blue) else: image.set(x, y, green) image.show()
3
0
TA
Please explain how you used the loop in #1 to solve the problem!
from cs1robots import * create_world() r=Robot(orientation='W', avenue=7, street=5) r.set_trace('red') while not r.facing_north(): r.turn_left() r.turn_left() while r.front_is_clear(): r.move() r.turn_left() while r.front_is_clear(): r.move() r.turn_left()
8
21
code_explain
The first loop was not given an initial direction, so the robot turned left until it looked north. The second and third loops were made to move forward just before the front was blocked by a wall.
cs1qa
null
null
null
null
null
Question: Please explain how you used the loop in #1 to solve the problem! Code: from cs1robots import * create_world() r=Robot(orientation='W', avenue=7, street=5) r.set_trace('red') while not r.facing_north(): r.turn_left() r.turn_left() while r.front_is_clear(): r.move() r.turn_left() while r.front_is_clear(): r.move() r.turn_left()
4
1
TA
When does the is_triangle() function return True in Task2?
def is_triangle(a, b, c): if a + b > c : if a + c > b : if b + c > a : print("YES") else : print("NO") else : print("NO") else : print("NO") return False a = float(input("Side a:")) b = float(input("Side b:")) c = float(input("Side c:")) is_triangle(a, b, c)
2
11
variable
I know it returns True when the print function is executed according to the conditional statement.
cs1qa
null
null
null
null
null
Question: When does the is_triangle() function return True in Task2? Code: def is_triangle(a, b, c): if a + b > c : if a + c > b : if b + c > a : print("YES") else : print("NO") else : print("NO") else : print("NO") return False a = float(input("Side a:")) b = float(input("Side b:")) c = float(input("Side c:")) is_triangle(a, b, c)
5
0
TA
What is the function of the global keyword in Task 1?
balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance print('You deposited %d won' % money) global balance balance=balance+money # Do something on here ! def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance print("You've withdrawn %d won" % money) if balance < money: print('But you only have %d won' % balance) return else: balance=balance-money # Do something on here ! def bank(): # Input : (None) No Input # Output : (None) No Output while True: global balance process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return elif process=='d': money=input('How much do you want to deposit?') deposit(int(money)) elif process=='w': money=input('How much do you want to withdraw?') withdrawal(int(money)) elif process=='c': print('Your current balance is %d won' % balance) else : print('Please press d or w or c or return') bank()
0
25
variable
Even if a variable has a value by operating within a function, it is impossible to use a local variable outside unless it is mapped to an external variable using return. By using the global keyword, the converted variable's value can be used outside the function.
cs1qa
null
null
null
null
null
Question: What is the function of the global keyword in Task 1? Code: balance = 0 def deposit(money): # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output # Add the money to the current balance print('You deposited %d won' % money) global balance balance=balance+money # Do something on here ! def withdrawal(money): # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output # Withdraw the money from the current balance global balance print("You've withdrawn %d won" % money) if balance < money: print('But you only have %d won' % balance) return else: balance=balance-money # Do something on here ! def bank(): # Input : (None) No Input # Output : (None) No Output while True: global balance process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return elif process=='d': money=input('How much do you want to deposit?') deposit(int(money)) elif process=='w': money=input('How much do you want to withdraw?') withdrawal(int(money)) elif process=='c': print('Your current balance is %d won' % balance) else : print('Please press d or w or c or return') bank()
5
0
TA
How did you use the balance variable in Task1?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance+=money print('You deposited', money, 'won') # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance if balance>=money: balance-=money print('You have withdraw', money, 'won') else: print('You have withdrawn', money, 'won') print('But you only have', balance, 'won') # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)?? ") if process=='c': print('Your current balance is', balance, 'won') elif process=='d': money=int(input('How much do you want to deposit?')) deposit(money) elif process=='w': money=int(input('How much do you want to withdraw?')) withdrawal(money) elif process=='': return else: print('Please, press d or w or c or none to quit') # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
5
5
variable
balance was considered and resolved as a balance
cs1qa
null
null
null
null
null
Question: How did you use the balance variable in Task1? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance balance+=money print('You deposited', money, 'won') # Add the money to the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance if balance>=money: balance-=money print('You have withdraw', money, 'won') else: print('You have withdrawn', money, 'won') print('But you only have', balance, 'won') # Withdraw the money from the current balance ################# ### implement ### ################# # Do something on here ! pass ################# def bank() : # Input : (None) No Input # Output : (None) No Output while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)?? ") if process=='c': print('Your current balance is', balance, 'won') elif process=='d': money=int(input('How much do you want to deposit?')) deposit(money) elif process=='w': money=int(input('How much do you want to withdraw?')) withdrawal(money) elif process=='': return else: print('Please, press d or w or c or none to quit') # If a user's input is empty string (''), then quit this function. # If a user's input is 'd', then ask the amount of money to deposit and deposit it. # If a user's input is 'w', then ask the amount of money to withdraw and withdraw it. # If a user's input is 'c', then check the current balance. ################# ### implement ### ################# # Do something on here ! pass ################# bank()
1
2
TA
Describe the trigger or intent you used both ascending and descending the climb_stair function in task 3.
from cs1robots import * #create_world() load_world('worlds/newspaper.wld') hubo = Robot(beepers=1) hubo.set_trace("blue") def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def climb_stair(): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() hubo.move() for i in range(4): climb_stair() hubo.drop_beeper() hubo.turn_left() hubo.turn_left() hubo.move() hubo.move() for i in range(3): climb_stair() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.turn_left()
17
26
reasoning
The order of actions when going up and down is the same, so I used it without distinction to reduce the length of the coded statement.
cs1qa
null
null
null
null
null
Question: Describe the trigger or intent you used both ascending and descending the climb_stair function in task 3. Code: from cs1robots import * #create_world() load_world('worlds/newspaper.wld') hubo = Robot(beepers=1) hubo.set_trace("blue") def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def climb_stair(): hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.move() hubo.move() for i in range(4): climb_stair() hubo.drop_beeper() hubo.turn_left() hubo.turn_left() hubo.move() hubo.move() for i in range(3): climb_stair() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.turn_left() hubo.turn_left()
4
1
TA
Why should we apply float() after receiving input() in Task 2?
global a, b, c def is_triangle(a, b, c): # print('Value of a is', a) # print('Value of b is', b) # print('Value of c is', c) if a>=b: if a>=c: if a>=(b+c): return False else: return True else: if c>=(a+b): return False else: return True else: if b>=c: if b>=(a+c): return False else: return True else: if c>=(a+b): return False else: return True a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) if is_triangle(a, b, c)==True: print('Yes') else: print('No')
31
33
reasoning
When it receives input, it recognizes the variable as a string and recognizes it as a number!
cs1qa
null
null
null
null
null
Question: Why should we apply float() after receiving input() in Task 2? Code: global a, b, c def is_triangle(a, b, c): # print('Value of a is', a) # print('Value of b is', b) # print('Value of c is', c) if a>=b: if a>=c: if a>=(b+c): return False else: return True else: if c>=(a+b): return False else: return True else: if b>=c: if b>=(a+c): return False else: return True else: if c>=(a+b): return False else: return True a = float(input('Side a: ')) b = float(input('Side b: ')) c = float(input('Side c: ')) if is_triangle(a, b, c)==True: print('Yes') else: print('No')
3
3
TA
How did you notice that there is a window in Task4?
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn_around(): hubo.turn_left() hubo.turn_left() hubo.move() hubo.drop_beeper() hubo.turn_left() hubo.move() while not hubo.on_beeper(): if hubo.front_is_clear(): if hubo.left_is_clear(): hubo.move() if hubo.left_is_clear(): turn_around() hubo.move() turn_right() else: turn_around() hubo.move() hubo.drop_beeper() turn_around() hubo.move() else: turn_right() hubo.pick_beeper() turn_right()
26
36
code_explain
In Task 4, the difference between the window and the simply empty left was judged by whether there was a wall on the left even when one more space was taken. Because if it were a window, there would have to be a wall in front of it.
cs1qa
null
null
null
null
null
Question: How did you notice that there is a window in Task4? Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain1.wld') # load_world('worlds/rain2.wld') # Initialize your robot at the door of the house. hubo = Robot(beepers=100, avenue=2, street=6, orientation='E') # Now close all the windows in the house! def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def turn_around(): hubo.turn_left() hubo.turn_left() hubo.move() hubo.drop_beeper() hubo.turn_left() hubo.move() while not hubo.on_beeper(): if hubo.front_is_clear(): if hubo.left_is_clear(): hubo.move() if hubo.left_is_clear(): turn_around() hubo.move() turn_right() else: turn_around() hubo.move() hubo.drop_beeper() turn_around() hubo.move() else: turn_right() hubo.pick_beeper() turn_right()
1
3
TA
Please explain this one() function too!
from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot() import time hubo.set_trace('blue') hubo.move() def one(): hubo.pick_beeper() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() hubo.turn_left() for i in range(3): one()
6
24
variable
Here, one() function refers to a function that moves once with the letter U, picks up a beeper, and moves one more space at the end.
cs1qa
null
null
null
null
null
Question: Please explain this one() function too! Code: from cs1robots import* load_world('worlds/harvest1.wld') hubo=Robot() import time hubo.set_trace('blue') hubo.move() def one(): hubo.pick_beeper() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() hubo.turn_left() for i in range(3): one()
2
3
TA
What does move_and_pick in Task 4 do?
from cs1robots import* load_world('worlds/harvest4.wld') hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range (3): hubo.turn_left() def move_and_pick(): hubo.move() if hubo.on_beeper(): while hubo.on_beeper(): hubo.pick_beeper() def harvesting(): for i in range(5): move_and_pick() hubo.turn_left() move_and_pick() hubo.turn_left() for i in range(5): move_and_pick() def turn_twice(): turn_right() move_and_pick() turn_right() move_and_pick() for i in range(2): harvesting() turn_twice() harvesting()
8
12
variable
Unlike task1, if there is a beeper, it uses a while statement to pick up the beeper until the beeper disappears in the corresponding place!
cs1qa
null
null
null
null
null
Question: What does move_and_pick in Task 4 do? Code: from cs1robots import* load_world('worlds/harvest4.wld') hubo=Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range (3): hubo.turn_left() def move_and_pick(): hubo.move() if hubo.on_beeper(): while hubo.on_beeper(): hubo.pick_beeper() def harvesting(): for i in range(5): move_and_pick() hubo.turn_left() move_and_pick() hubo.turn_left() for i in range(5): move_and_pick() def turn_twice(): turn_right() move_and_pick() turn_right() move_and_pick() for i in range(2): harvesting() turn_twice() harvesting()
2
0
TA
I'm going to check it now. Can you explain how you made it for each task in the meantime?
from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace("blue") def harvest(): if hubo.on_beeper(): hubo.pick_beeper() def st1(): for i in range(5): hubo.move() harvest() hubo.turn_left() hubo.move() harvest() hubo.turn_left() for i in range(5): hubo.move() harvest() for i in range(3): hubo.turn_left() hubo.move() harvest() for i in range(3): hubo.turn_left() hubo.move() harvest() for i in range(2): st1() for i in range(5): hubo.move() harvest() hubo.turn_left() hubo.move() harvest() hubo.turn_left() for i in range(5): hubo.move() harvest()
0
40
code_explain
task1 wrote a program that moves all paths with beepers and then wrote a separate function that collects beepers when there is a beeper in each moving state, so that the function works after every move.
cs1qa
null
null
null
null
null
Question: I'm going to check it now. Can you explain how you made it for each task in the meantime? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo=Robot() hubo.set_trace("blue") def harvest(): if hubo.on_beeper(): hubo.pick_beeper() def st1(): for i in range(5): hubo.move() harvest() hubo.turn_left() hubo.move() harvest() hubo.turn_left() for i in range(5): hubo.move() harvest() for i in range(3): hubo.turn_left() hubo.move() harvest() for i in range(3): hubo.turn_left() hubo.move() harvest() for i in range(2): st1() for i in range(5): hubo.move() harvest() hubo.turn_left() hubo.move() harvest() hubo.turn_left() for i in range(5): hubo.move() harvest()
1
0
TA
Please explain the zigzag function of task1.
from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def go_straight(): for i in range(9): hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def zigzag(): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
10
16
variable
In order to use the for statement for the robot, the zigzag function defines a zigzag function that rotates left, goes up, goes right, goes forward once, goes right, goes down again, and descends back to the last line.
cs1qa
null
null
null
null
null
Question: Please explain the zigzag function of task1. Code: from cs1robots import * create_world() hubo=Robot() hubo.set_trace('blue') def go_straight(): for i in range(9): hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def zigzag(): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() for i in range(4): zigzag() hubo.turn_left() hubo.move() zigzag()
6
2
student
What does it mean to have two brackets like cards[i][0]?
from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def show(): w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 def hide(): w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if is_valid(num1, num2): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if num1 in correct_list or num2 in correct_list: return False elif num1 == num2: return False elif 0 <= num1 <=23 and 0<= num2<=23: return True ########################################################################### def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. if is_valid(num1, num2): canvas.add(cards[num1%6][num1//6]) canvas.remove(num_pads[num1]) canvas.add(cards[num2%6][num2//6]) canvas.remove(num_pads[num2]) time.sleep(1) if cards[num1]==cards[num2]: correct_list.append(cards[num1]) correct_list.append(cards[num2]) return True else: canvas.remove(cards[num1%6][num1//6]) canvas.add(num_pads[num1]) canvas.remove(cards[num2%6][num2//6]) canvas.add(num_pads[num2]) return False ########################################################################### initialize() show() time.sleep(3) hide() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not len(cards) == 0: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs t=str(tries) if t.endswith("1"): if t==11: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif t.endswith("2"): if t==12: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif t.endswith("3"): if t==13: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries= tries + 1 ###########################################################################
43
43
code_understanding
There must be another iterable object in the iterable object, right?x = [(1,2), (3,4)] When x [0][1] = 2
cs1qa
null
null
null
null
null
Question: What does it mean to have two brackets like cards[i][0]? Code: from cs1graphics import * import time import random canvas = Canvas(640, 580) canvas.setTitle("Memento") path = "./images/" names = ("Dohoo.jpg", "Jeongmin.jpg", "Jinyeong.jpg", "Minsuk.jpg", "Sangjae.jpg", "Sungeun.jpg") cards = [] num_pads = [] tries = 0 correct_list = [] def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = (img, names[i]) cards.append(temp_tuple) for i in range(24): card = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) card.add(rect) card.add(text) num_pads.append(card) ################################################################ # 3-2-1. shuffle the card list random.shuffle(cards) ################################################################ def show(): w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 def hide(): w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) cards[i][0].moveTo(i_w + w, i_h+h) canvas.remove(cards[i][0]) w += 100 if w % 600 == 0: w = 0 h += 130 def print_cards(): canvas.clear() w = 0 h = 0 i_w = 70 i_h = 90 for i in range(len(num_pads)): ################################################################ if is_valid(num1, num2): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h+h) canvas.add(cards[i][0]) else: num_pads[i].moveTo(i_w + w, i_h+h) canvas.add(num_pads[i]) w += 100 if w % 600 == 0: w = 0 h += 130 time.sleep(1) def is_valid(num1, num2): ########################################################################### # 3-1-1. Check if any of two numbers exists in the current correct list, # two numbers are the same, # or both of the numbers are within a valid range. # Return Boolean value according to the result. if num1 in correct_list or num2 in correct_list: return False elif num1 == num2: return False elif 0 <= num1 <=23 and 0<= num2<=23: return True ########################################################################### def check(num1, num2): ########################################################################### # 3-1-2. At first, visualize the screen including the two cards # (num1-th card and num2-th card). # If two pictures of the two cards are same, # put two numbers into the correct list. # If not, re-visualize the original screen. # Return Boolean value according to the result. if is_valid(num1, num2): canvas.add(cards[num1%6][num1//6]) canvas.remove(num_pads[num1]) canvas.add(cards[num2%6][num2//6]) canvas.remove(num_pads[num2]) time.sleep(1) if cards[num1]==cards[num2]: correct_list.append(cards[num1]) correct_list.append(cards[num2]) return True else: canvas.remove(cards[num1%6][num1//6]) canvas.add(num_pads[num1]) canvas.remove(cards[num2%6][num2//6]) canvas.add(num_pads[num2]) return False ########################################################################### initialize() show() time.sleep(3) hide() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not len(cards) == 0: # 3-2-3. Rewrite the condition for termination ############################################################################### ########################################################################### # 3-2-4. Print the number of tries and the corrected pairs t=str(tries) if t.endswith("1"): if t==11: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") elif t.endswith("2"): if t==12: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") elif t.endswith("3"): if t==13: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") else: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue if check(num1, num2): print("Correct!") else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries= tries + 1 ###########################################################################
3
3
TA
Please explain why you created the check_beeper_all() function and what the function does.
from cs1robots import * load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() # Check whether the beepers exist and harvest them all def check_beeper_all(): while hubo.on_beeper(): hubo.pick_beeper() # Scans 2 floors(rows) which have {num} columns and harvest all the beepers def scan_two_floor(num): for k in range(num - 1): check_beeper_all() hubo.move() check_beeper_all() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): check_beeper_all() hubo.move() check_beeper_all() # Input the number of row and column of check area,{row},{col} each def harvest_even_more(row, col): itr = int(row / 2) for k in range(itr): scan_two_floor(col) if k != itr -1: turn_right() hubo.move() turn_right() if row % 2 == 1: # If number of the row is odd turn_right() hubo.move() turn_right() for k in range(col - 1): check_beeper_all() hubo.move() check_beeper_all() hubo.move() harvest_even_more(6,6)
10
12
reasoning
If there are multiple beepers, I made them all to harvest.
cs1qa
null
null
null
null
null
Question: Please explain why you created the check_beeper_all() function and what the function does. Code: from cs1robots import * load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() # Check whether the beepers exist and harvest them all def check_beeper_all(): while hubo.on_beeper(): hubo.pick_beeper() # Scans 2 floors(rows) which have {num} columns and harvest all the beepers def scan_two_floor(num): for k in range(num - 1): check_beeper_all() hubo.move() check_beeper_all() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): check_beeper_all() hubo.move() check_beeper_all() # Input the number of row and column of check area,{row},{col} each def harvest_even_more(row, col): itr = int(row / 2) for k in range(itr): scan_two_floor(col) if k != itr -1: turn_right() hubo.move() turn_right() if row % 2 == 1: # If number of the row is odd turn_right() hubo.move() turn_right() for k in range(col - 1): check_beeper_all() hubo.move() check_beeper_all() hubo.move() harvest_even_more(6,6)
8
1
TA
In Task2, when you split, you split with ", not ". What is the reason?
file=open('average-latitude-longitude-countries.csv',"r") list_all=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_all.append((list_splited[1],list_splited[3],float(list_splited[4].split(",")[1]), float(list_splited[4].split(",")[2]))) file.close() file=open('average-latitude-longitude-countries.csv',"r") list_cc_cn=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_cc_cn.append((list_splited[1],list_splited[3])) print(list_cc_cn) file.close() file=open('average-latitude-longitude-countries.csv',"r") list_cc_ll=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_cc_ll.append((list_splited[1],(float(list_splited[4].split(",")[1]), float(list_splited[4].split(",")[2])))) print(list_cc_ll) file.close() for item in list_cc_ll: if item[1][0]<0: cc=item[0] for items in list_all: if items[0]==cc: print(items[1]) while True: cc=input("Enter Country code: ") for items in list_all: if items[0]==cc: print(items[1])
24
24
reasoning
This is to return a new list divided by "
cs1qa
null
null
null
null
null
Question: In Task2, when you split, you split with ", not ". What is the reason? Code: file=open('average-latitude-longitude-countries.csv',"r") list_all=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_all.append((list_splited[1],list_splited[3],float(list_splited[4].split(",")[1]), float(list_splited[4].split(",")[2]))) file.close() file=open('average-latitude-longitude-countries.csv',"r") list_cc_cn=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_cc_cn.append((list_splited[1],list_splited[3])) print(list_cc_cn) file.close() file=open('average-latitude-longitude-countries.csv',"r") list_cc_ll=[] for line in file: if line.strip()=='"ISO 3166 Country Code","Country","Latitude","Longitude"': continue list_splited=line.strip().split('"') list_cc_ll.append((list_splited[1],(float(list_splited[4].split(",")[1]), float(list_splited[4].split(",")[2])))) print(list_cc_ll) file.close() for item in list_cc_ll: if item[1][0]<0: cc=item[0] for items in list_all: if items[0]==cc: print(items[1]) while True: cc=input("Enter Country code: ") for items in list_all: if items[0]==cc: print(items[1])
1
2
TA
Did you deliberately write the 23rd line in task3?
from cs1robots import * load_world('worlds/newspaper.wld') hubo = Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def climb_once(): hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() for k in range(4): climb_once() hubo.move() hubo.drop_beeper() hubo.turn_left(); hubo.turn_left(); hubo.move(); for k in range(4): climb_once()
22
22
reasoning
Yes, that is correct!
cs1qa
null
null
null
null
null
Question: Did you deliberately write the 23rd line in task3? Code: from cs1robots import * load_world('worlds/newspaper.wld') hubo = Robot(beepers=1) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def climb_once(): hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() for k in range(4): climb_once() hubo.move() hubo.drop_beeper() hubo.turn_left(); hubo.turn_left(); hubo.move(); for k in range(4): climb_once()
10
0
TA
How did you implement the interaction
from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ sun = Circle(18, Point(430, 220)) sun.setFillColor('yellow') sun.setDepth(30) _scene.add(sun) flare = Circle(24, Point(430, 220)) flare.setFillColor('red') flare.setDepth(40) _scene.add(flare) class ship (object): def __init__(self, x, y, color): self.ship = Layer() body = Rectangle(28, 10, Point(x, y)) body.setFillColor(color) body.setBorderColor(color) self.ship.add(body) self.ball1 = Circle(8, Point(x, y+15)) self.ball2 = Circle(8, Point(x, y-25)) self.ball1.setFillColor('yellow') self.ball2.setFillColor('yellow') self.ship.add(self.ball1) self.ship.add(self.ball2) self.ship.setDepth(10) _scene.add(self.ship) def shake(self): self.ball1.move(0, 10) self.ball2.move(0, 10) sleep(0.5) self.ball1.move(0, -10) self.ball2.move(0, -10) def move(self, a): for j in range(a): self.ship.move(10, 0) sleep(0.2) def blink(self): for j in range(10): self.ball1.setFillColor('white') self.ball2.setFillColor('white') sleep(0.1) self.ball1.setFillColor('yellow') self.ball2.setFillColor('yellow') sleep(0.1) create_world() # define your objects, e.g. mario = Mario('blue', 'normal') rocket = ship(100, 110, 'blue') def show_animation(): # write your animation scenario here for p in range(10): rocket.shake() rocket.move(1) rocket.blink() for q in range(20): rocket.shake() rocket.move(1) ## show_animation() def interactive(): while True: e = _scene.wait() d = e.getDescription() if d=="keyboard": key = e.getKey() if key=='q': _scene.close() break elif key=='b': rocket.blink() elif key=='w': rocket.move(1) interactive()
98
110
code_explain
q is the shutdown command, b is the blink method of the ship class, and w is the move method of the ship class. The command is input with the getKey() method.
cs1qa
null
null
null
null
null
Question: How did you implement the interaction Code: from cs1graphics import * from time import sleep _scene = None _world = None def create_world(): global _scene, _world if _scene: raise RuntimeError("A world already exists!") _world = _World(500, 300) _scene = Canvas(_world.width, _world.height) _scene.setTitle("Mario World") _world.draw_scene() class _World(object): def __init__(self, width, height): self.width = width self.height = height def draw_scene(self): """ draw background here Don't forget _scene.add(name) """ sun = Circle(18, Point(430, 220)) sun.setFillColor('yellow') sun.setDepth(30) _scene.add(sun) flare = Circle(24, Point(430, 220)) flare.setFillColor('red') flare.setDepth(40) _scene.add(flare) class ship (object): def __init__(self, x, y, color): self.ship = Layer() body = Rectangle(28, 10, Point(x, y)) body.setFillColor(color) body.setBorderColor(color) self.ship.add(body) self.ball1 = Circle(8, Point(x, y+15)) self.ball2 = Circle(8, Point(x, y-25)) self.ball1.setFillColor('yellow') self.ball2.setFillColor('yellow') self.ship.add(self.ball1) self.ship.add(self.ball2) self.ship.setDepth(10) _scene.add(self.ship) def shake(self): self.ball1.move(0, 10) self.ball2.move(0, 10) sleep(0.5) self.ball1.move(0, -10) self.ball2.move(0, -10) def move(self, a): for j in range(a): self.ship.move(10, 0) sleep(0.2) def blink(self): for j in range(10): self.ball1.setFillColor('white') self.ball2.setFillColor('white') sleep(0.1) self.ball1.setFillColor('yellow') self.ball2.setFillColor('yellow') sleep(0.1) create_world() # define your objects, e.g. mario = Mario('blue', 'normal') rocket = ship(100, 110, 'blue') def show_animation(): # write your animation scenario here for p in range(10): rocket.shake() rocket.move(1) rocket.blink() for q in range(20): rocket.shake() rocket.move(1) ## show_animation() def interactive(): while True: e = _scene.wait() d = e.getDescription() if d=="keyboard": key = e.getKey() if key=='q': _scene.close() break elif key=='b': rocket.blink() elif key=='w': rocket.move(1) interactive()
2
4
TA
What function shuttle_run does in Task 5?
from cs1robots import * # Your code must work for empty worlds of all possible sizes. # create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) create_world(avenues=12, streets=3) # ... my_robot = Robot() my_robot.set_trace("blue") def turn_right(): for i in range(3): my_robot.turn_left() def shuttle_run(): my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_right() if my_robot.front_is_clear(): my_robot.move() turn_right() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() shuttle_run() while my_robot.front_is_clear(): my_robot.move() shuttle_run() #make robot look forward at final position turn_right()
20
30
variable
This function is set to go up, move one line to the right in front of the wall and come down again. However, because the number of passages has not been decided If it becomes impossible to move one line to the right from the top, it is set to stop at that position.
cs1qa
null
null
null
null
null
Question: What function shuttle_run does in Task 5? Code: from cs1robots import * # Your code must work for empty worlds of all possible sizes. # create_world(avenues=10, streets=10) # create_world(avenues=11, streets=8) # create_world(avenues=6, streets=9) # create_world(avenues=1, streets=3) # create_world(avenues=2, streets=1) # create_world(avenues=1, streets=2) create_world(avenues=12, streets=3) # ... my_robot = Robot() my_robot.set_trace("blue") def turn_right(): for i in range(3): my_robot.turn_left() def shuttle_run(): my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() turn_right() if my_robot.front_is_clear(): my_robot.move() turn_right() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() shuttle_run() while my_robot.front_is_clear(): my_robot.move() shuttle_run() #make robot look forward at final position turn_right()
1
3
TA
What is the reason for writing odd_line and even_line?
from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.move() hubo.pick_beeper() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def one_line_harvest(): for i in range(5): hubo.move() hubo.pick_beeper() def even_line_harvest(): one_line_harvest() turn_right() hubo.move() turn_right() hubo.pick_beeper() def odd_line_harvest(): one_line_harvest() hubo.turn_left() hubo.move() hubo.turn_left() hubo.pick_beeper() def two_lines_harvest(): even_line_harvest() odd_line_harvest() two_lines_harvest() two_lines_harvest() two_lines_harvest()
13
24
reasoning
In even-numbered lines, it moves one line and then turns to the right, but in odd-numbered lines, it moves one line and turns to the left, so I wrote a difference.
cs1qa
null
null
null
null
null
Question: What is the reason for writing odd_line and even_line? Code: from cs1robots import * load_world('worlds/harvest1.wld') hubo=Robot() hubo.move() hubo.pick_beeper() hubo.turn_left() def turn_right(): for i in range(3): hubo.turn_left() def one_line_harvest(): for i in range(5): hubo.move() hubo.pick_beeper() def even_line_harvest(): one_line_harvest() turn_right() hubo.move() turn_right() hubo.pick_beeper() def odd_line_harvest(): one_line_harvest() hubo.turn_left() hubo.move() hubo.turn_left() hubo.pick_beeper() def two_lines_harvest(): even_line_harvest() odd_line_harvest() two_lines_harvest() two_lines_harvest() two_lines_harvest()
2
3
TA
Can you explain task 4
from cs1robots import* load_world('worlds/harvest3.wld') hubo=Robot() import time hubo.set_trace('blue') def pick_and_drop(): while hubo.on_beeper(): hubo.pick_beeper() def turn_right(): for i in range(3): hubo.turn_left() hubo.move() def one(): pick_and_drop() for i in range(5): hubo.move() pick_and_drop() hubo.turn_left() hubo.move() pick_and_drop() hubo.turn_left() for i in range(5): hubo.move() pick_and_drop() turn_right() hubo.move() turn_right() for i in range(3): one()
13
30
code_explain
Task 4 uses a while statement in the pick_and_drop function to pick up multiple beepers until they disappear.
cs1qa
null
null
null
null
null
Question: Can you explain task 4 Code: from cs1robots import* load_world('worlds/harvest3.wld') hubo=Robot() import time hubo.set_trace('blue') def pick_and_drop(): while hubo.on_beeper(): hubo.pick_beeper() def turn_right(): for i in range(3): hubo.turn_left() hubo.move() def one(): pick_and_drop() for i in range(5): hubo.move() pick_and_drop() hubo.turn_left() hubo.move() pick_and_drop() hubo.turn_left() for i in range(5): hubo.move() pick_and_drop() turn_right() hubo.move() turn_right() for i in range(3): one()