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
3
TA
Could you explain task 4's block function?
from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain2.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') hubo.set_trace('red') # Now close all the windows in the house! def turn_right(): for i in range(3): hubo.turn_left() def block(): while(hubo.front_is_clear()): hubo.move() if(hubo.left_is_clear()): hubo.turn_left() hubo.move() if(hubo.front_is_clear()): hubo.drop_beeper() hubo.move() hubo.drop_beeper() hubo.turn_left() block() turn_right() block() turn_right() block() turn_right() block() turn_right() hubo.move() hubo.turn_left() block() turn_right() while(not hubo.on_beeper()): hubo.move() hubo.pick_beeper() hubo.turn_left()
14
21
variable
I chose a clockwise rotation method, in which case the block is a function that makes the beeper drop when there is no wall (when the window is open) while going straight along one side (until it is blocked by the other side)
cs1qa
null
null
null
null
null
Question: Could you explain task 4's block function? Code: from cs1robots import * # Your code must work for both of the worlds below. load_world('worlds/rain2.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') hubo.set_trace('red') # Now close all the windows in the house! def turn_right(): for i in range(3): hubo.turn_left() def block(): while(hubo.front_is_clear()): hubo.move() if(hubo.left_is_clear()): hubo.turn_left() hubo.move() if(hubo.front_is_clear()): hubo.drop_beeper() hubo.move() hubo.drop_beeper() hubo.turn_left() block() turn_right() block() turn_right() block() turn_right() block() turn_right() hubo.move() hubo.turn_left() block() turn_right() while(not hubo.on_beeper()): hubo.move() hubo.pick_beeper() hubo.turn_left()
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
2
TA
Please briefly explain the functions up and down that you wrote down in number 3 as well!
from cs1robots import* load_world('worlds/newspaper.wld') hubo=Robot(beepers=10) hubo.set_trace('blue') def up(): for i in range(4): hubo.move() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo.move() up() hubo.move() hubo.drop_beeper() def down(): for i in range(4): hubo.move() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(2): hubo.turn_left() hubo.move() down()
4
22
variable
Up is a function that represents going up all the stairs, so we repeated four times to go up one stair with a for statement, and because down is a function that indicates going down all stairs, we repeated going down one stair with a for statement.
cs1qa
null
null
null
null
null
Question: Please briefly explain the functions up and down that you wrote down in number 3 as well! Code: from cs1robots import* load_world('worlds/newspaper.wld') hubo=Robot(beepers=10) hubo.set_trace('blue') def up(): for i in range(4): hubo.move() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo.move() up() hubo.move() hubo.drop_beeper() def down(): for i in range(4): hubo.move() hubo.turn_left() hubo.move() for i in range(3): hubo.turn_left() hubo.move() for i in range(2): hubo.turn_left() hubo.move() down()
6
2
student
Why does!
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 and num2 in correct_list: return False elif num1 == num2: 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 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) ###########################################################################
93
108
code_understanding
Because the while statement has to run until correct_list does not contain all the numbers I made it not equal
cs1qa
null
null
null
null
null
Question: Why does! 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 and num2 in correct_list: return False elif num1 == num2: 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 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) ###########################################################################
3
1
TA
Please tell me about the function and role of the plant() function.
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot(beepers=100) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def plant_beeper(): # If hubo is not on beeper, plant a beeper if not hubo.on_beeper(): hubo.drop_beeper() # Scans 2 floors(rows) which have {num} columns and if there is no beeper, then plant it def scan_two_floor(num): for k in range(num - 1): plant_beeper() hubo.move() plant_beeper() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): plant_beeper() hubo.move() plant_beeper() # Input the number of row and column of check area,{row},{col} each def plant(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): plant_beeper() hubo.move() plant_beeper() hubo.move() plant(6,6)
9
44
variable
This is the code that checks if there is no beeper where the robot is located, and if not
cs1qa
null
null
null
null
null
Question: Please tell me about the function and role of the plant() function. Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot(beepers=100) hubo.set_trace("blue") def turn_right(): for i in range(3): hubo.turn_left() def plant_beeper(): # If hubo is not on beeper, plant a beeper if not hubo.on_beeper(): hubo.drop_beeper() # Scans 2 floors(rows) which have {num} columns and if there is no beeper, then plant it def scan_two_floor(num): for k in range(num - 1): plant_beeper() hubo.move() plant_beeper() hubo.turn_left() if hubo.front_is_clear(): hubo.move() hubo.turn_left() for k in range(num - 1): plant_beeper() hubo.move() plant_beeper() # Input the number of row and column of check area,{row},{col} each def plant(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): plant_beeper() hubo.move() plant_beeper() hubo.move() plant(6,6)
1
4
TA
What is the role of carrots?
from cs1robots import * load_world("worlds/harvest2.wld") hubo = Robot() carrots = 0 marker = 0 hubo.set_trace("blue") ##optional def turnright(): for i in range(3): hubo.turn_left() def proceed(): for i in range(2): hubo.move() #before collecting halfway def righty(): global carrots hubo.pick_beeper() hubo.move() turnright() hubo.move() hubo.pick_beeper() turnright() carrots += 2 def lefty(): global carrots hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() carrots += 2 #after collecting halfway def righty2(): global carrots proceed() hubo.pick_beeper() turnright() hubo.move() turnright() hubo.move() def lefty2(): global carrots proceed() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def initial(): hubo.turn_left() for i in range(6): hubo.move() def execute(): global carrots, marker initial() while carrots < 36 : #proceed, pick up, proceed -- numbers go like 123454321 for i in range(10): if marker%2 == 0 and marker < 5: righty() elif marker%2 == 1 and marker < 5: lefty() elif marker%2 == 0 and marker >= 5: righty2() elif marker%2 == 1 and marker >= 5: lefty2() #proceed() if i == 0 : proceed() pass elif i >= 1 and i < 4 : proceed() for j in range(i): hubo.pick_beeper() carrots += 1 proceed() elif i == 4 : for j in range(4): proceed() hubo.pick_beeper() carrots += 1 elif i >= 5 and i < 8 : for j in range(8-i): hubo.pick_beeper() carrots += 1 proceed() hubo.pick_beeper() elif i >= 8 : hubo.pick_beeper() if i == 9: exit() marker += 1 execute()
61
97
variable
carrots is the number of beepers the robot has collected
cs1qa
null
null
null
null
null
Question: What is the role of carrots? Code: from cs1robots import * load_world("worlds/harvest2.wld") hubo = Robot() carrots = 0 marker = 0 hubo.set_trace("blue") ##optional def turnright(): for i in range(3): hubo.turn_left() def proceed(): for i in range(2): hubo.move() #before collecting halfway def righty(): global carrots hubo.pick_beeper() hubo.move() turnright() hubo.move() hubo.pick_beeper() turnright() carrots += 2 def lefty(): global carrots hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() hubo.turn_left() carrots += 2 #after collecting halfway def righty2(): global carrots proceed() hubo.pick_beeper() turnright() hubo.move() turnright() hubo.move() def lefty2(): global carrots proceed() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def initial(): hubo.turn_left() for i in range(6): hubo.move() def execute(): global carrots, marker initial() while carrots < 36 : #proceed, pick up, proceed -- numbers go like 123454321 for i in range(10): if marker%2 == 0 and marker < 5: righty() elif marker%2 == 1 and marker < 5: lefty() elif marker%2 == 0 and marker >= 5: righty2() elif marker%2 == 1 and marker >= 5: lefty2() #proceed() if i == 0 : proceed() pass elif i >= 1 and i < 4 : proceed() for j in range(i): hubo.pick_beeper() carrots += 1 proceed() elif i == 4 : for j in range(4): proceed() hubo.pick_beeper() carrots += 1 elif i >= 5 and i < 8 : for j in range(8-i): hubo.pick_beeper() carrots += 1 proceed() hubo.pick_beeper() elif i >= 8 : hubo.pick_beeper() if i == 9: exit() marker += 1 execute()
1
0
TA
Please briefly explain what a lambda function is
from cs1robots import * create_world() rb = Robot() # \Robo_HEAD/ def rpt(fu, i, args=()): # repeat for x in range(i): fu.__call__(*args) tl = lambda : rb.turn_left() tr = lambda : rpt(rb.turn_left, 3) mv = lambda i : rpt(rb.move, i) def setup(): tl() rb.set_trace("blue") def foo(): mv(9); tr(); mv(1); tr(); mv(9); def bar(): tl(); mv(1); tl(); def foobar(): foo(); bar(); if __name__=="__main__": setup() rpt(foobar, 4) foo()
8
10
variable
Anonymous function, function without name It is derived from the lambda calculation method suggested by Alonzo Church. If func1 = lambda x: 2x def func1(x): return 2*x I feel like
cs1qa
null
null
null
null
null
Question: Please briefly explain what a lambda function is Code: from cs1robots import * create_world() rb = Robot() # \Robo_HEAD/ def rpt(fu, i, args=()): # repeat for x in range(i): fu.__call__(*args) tl = lambda : rb.turn_left() tr = lambda : rpt(rb.turn_left, 3) mv = lambda i : rpt(rb.move, i) def setup(): tl() rb.set_trace("blue") def foo(): mv(9); tr(); mv(1); tr(); mv(9); def bar(): tl(); mv(1); tl(); def foobar(): foo(); bar(); if __name__=="__main__": setup() rpt(foobar, 4) foo()
3
0
TA
Explain lines 8-9 in task1
from cs1robots import * create_world() my_robot = Robot(orientation='W', avenue=7, street=5) my_robot.set_trace('blue') while not my_robot.facing_north(): my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left()
7
8
code_explain
Turn until you see north, and when you see north, you look west by line 10
cs1qa
null
null
null
null
null
Question: Explain lines 8-9 in task1 Code: from cs1robots import * create_world() my_robot = Robot(orientation='W', avenue=7, street=5) my_robot.set_trace('blue') while not my_robot.facing_north(): my_robot.turn_left() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left()
2
0
TA
What is the role of the move1() 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 move1(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def lap(): for i in range(5): move1() hubo.turn_left() move1() hubo.turn_left() for i in range(5): move1() move1() lap() for i in range(2): turn_right() move1() turn_right() lap()
9
12
variable
The move1() function is to pick up the beeper if the robot moves and there is a beeper at that location.
cs1qa
null
null
null
null
null
Question: What is the role of the move1() 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 move1(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def lap(): for i in range(5): move1() hubo.turn_left() move1() hubo.turn_left() for i in range(5): move1() move1() lap() for i in range(2): turn_right() move1() turn_right() lap()
2
3
TA
Explain the farmer function in task 4
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 harvest(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() def farmer(): for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() turn_right() harvest() turn_right() for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() farmer() farmer() for i in range(5): harvest()
24
38
variable
And the farmer function is defined to pick up the beeper by continuing to execute the harvest function, a function that picks up the beeper, while moving in zigzag while moving one line to the right and one line to the left as a unit.
cs1qa
null
null
null
null
null
Question: Explain the farmer function in task 4 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 harvest(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() def farmer(): for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() turn_right() harvest() turn_right() for i in range(5): harvest() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() harvest() hubo.turn_left() farmer() farmer() for i in range(5): harvest()
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')
1
14
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')
4
3
TA
Why do you add 40 to the end of line 9 in the 4th task?
import math sin = math.sin pi = math.pi cos = math.cos for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = math.sin(x)+40*sin(x)+40# Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
7
10
reasoning
I decided on the function, but I used trial and error a bit, but the first line was 40 #s.
cs1qa
null
null
null
null
null
Question: Why do you add 40 to the end of line 9 in the 4th task? Code: import math sin = math.sin pi = math.pi cos = math.cos for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = math.sin(x)+40*sin(x)+40# Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
5
1
student
Are geometry objects like tiger and sq_n also variables?
from cs1graphics import * from time import sleep world=Canvas(1200,1000) world.setBackgroundColor("light blue") world.setTitle("CS101 LAB5 Animation") def draw_animal(): global tiger global sq1 global sq2 global sq3 global sq4 global sq5 tiger=Layer() sq1=Rectangle(160,75, Point(-80,-100)) tiger.add(sq1) sq2=Rectangle(20,120, Point(-40,-60)) tiger.add(sq2) sq3=Rectangle(20,120, Point(-120,-60)) tiger.add(sq3) sq4=Square(50, Point(-170,-150)) tiger.add(sq4) sq5=Square(5, Point(-185,-160)) tiger.add(sq5) tiger.moveTo(1000,900) world.add(tiger) sq1.setFillColor("white") sq2.setFillColor("white") sq3.setFillColor("white") sq4.setFillColor("white") sq5.setFillColor("black") sq1.setDepth(3) sq2.setDepth(4) sq3.setDepth(4) sq4.setDepth(2) sq5.setDepth(1) def show_animation(): global tiger for i in range(100): tiger.move(-10,0) sq2.rotate(30) sq3.rotate(30) sleep(0.01) sq2.rotate(-60) sq3.rotate(-60) sleep(0.01) sq2.rotate(30) sq3.rotate(30) sleep(0.01) draw_animal() show_animation()
13
22
code_understanding
Oh, it can be called a variable
cs1qa
null
null
null
null
null
Question: Are geometry objects like tiger and sq_n also variables? Code: from cs1graphics import * from time import sleep world=Canvas(1200,1000) world.setBackgroundColor("light blue") world.setTitle("CS101 LAB5 Animation") def draw_animal(): global tiger global sq1 global sq2 global sq3 global sq4 global sq5 tiger=Layer() sq1=Rectangle(160,75, Point(-80,-100)) tiger.add(sq1) sq2=Rectangle(20,120, Point(-40,-60)) tiger.add(sq2) sq3=Rectangle(20,120, Point(-120,-60)) tiger.add(sq3) sq4=Square(50, Point(-170,-150)) tiger.add(sq4) sq5=Square(5, Point(-185,-160)) tiger.add(sq5) tiger.moveTo(1000,900) world.add(tiger) sq1.setFillColor("white") sq2.setFillColor("white") sq3.setFillColor("white") sq4.setFillColor("white") sq5.setFillColor("black") sq1.setDepth(3) sq2.setDepth(4) sq3.setDepth(4) sq4.setDepth(2) sq5.setDepth(1) def show_animation(): global tiger for i in range(100): tiger.move(-10,0) sq2.rotate(30) sq3.rotate(30) sleep(0.01) sq2.rotate(-60) sq3.rotate(-60) sleep(0.01) sq2.rotate(30) sq3.rotate(30) sleep(0.01) draw_animal() show_animation()
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') class Card: def __init__(self,suit,face,value,image,state): assert face in face_names and suit in suit_names self.face=face self.suit=suit self.value=value self.image=image self.state=state def create_deck(): deck=[] for suit in suit_names: for face in face_names: img_name=img_path+suit+'_'+face+'.png' deck.append(Card(suit,face,value[face_names.index(face)],img_name,True)) random.shuffle(deck) return deck def hand_value(hand): value=0 for i in hand: value=value+i.value return value def card_string(card): if card.face=='Ace': return 'An '+card.face+' of '+card.suit else: return 'A '+card.face+' of '+card.suit def ask_yesno(prompt): while prompt!='y' and prompt!='n': prompt=input("\nPlay another round? (y/n) ") if prompt=='y': return True else: return False def draw_card(dealer,player): depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in player: count_i=20*player.index(i) j=Image(i.image) j.moveTo(100+count_i,300) j.setDepth(depth-count_i) bj_board.add(j) text1=Text('Your Total: '+str(hand_value(player)),12,None) text1.moveTo(500,300) text1.setFontColor('yellow') bj_board.add(text1) for k in dealer: count_k=20*dealer.index(k) if k==dealer[0]: if k.state==False: k.image='./images/Back.png' else: k.image=img_path+k.suit+'_'+k.face+'.png' else: pass j=Image(k.image) j.moveTo(100+count_k,100) j.setDepth(depth-count_k) bj_board.add(j) if dealer[0].state==True: text2=Text("The dealer's Total: "+str(hand_value(dealer)),12,None) text2.moveTo(500,100) text2.setFontColor('yellow') bj_board.add(text2) else: pass count_i=count_i+10 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()
16
16
variable
assert checks whether the face_names list and the suit_names list contain faces and suits!
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') class Card: def __init__(self,suit,face,value,image,state): assert face in face_names and suit in suit_names self.face=face self.suit=suit self.value=value self.image=image self.state=state def create_deck(): deck=[] for suit in suit_names: for face in face_names: img_name=img_path+suit+'_'+face+'.png' deck.append(Card(suit,face,value[face_names.index(face)],img_name,True)) random.shuffle(deck) return deck def hand_value(hand): value=0 for i in hand: value=value+i.value return value def card_string(card): if card.face=='Ace': return 'An '+card.face+' of '+card.suit else: return 'A '+card.face+' of '+card.suit def ask_yesno(prompt): while prompt!='y' and prompt!='n': prompt=input("\nPlay another round? (y/n) ") if prompt=='y': return True else: return False def draw_card(dealer,player): depth = 100 x0,y0 = 100,100 x1,y1 = 100,300 bj_board.clear() for i in player: count_i=20*player.index(i) j=Image(i.image) j.moveTo(100+count_i,300) j.setDepth(depth-count_i) bj_board.add(j) text1=Text('Your Total: '+str(hand_value(player)),12,None) text1.moveTo(500,300) text1.setFontColor('yellow') bj_board.add(text1) for k in dealer: count_k=20*dealer.index(k) if k==dealer[0]: if k.state==False: k.image='./images/Back.png' else: k.image=img_path+k.suit+'_'+k.face+'.png' else: pass j=Image(k.image) j.moveTo(100+count_k,100) j.setDepth(depth-count_k) bj_board.add(j) if dealer[0].state==True: text2=Text("The dealer's Total: "+str(hand_value(dealer)),12,None) text2.moveTo(500,100) text2.setFontColor('yellow') bj_board.add(text2) else: pass count_i=count_i+10 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()
1
4
TA
Could you briefly explain the harvest up and down functions?
from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace("blue") #hubo.set_pause(1) def turn_right(): for i in range(3): hubo.turn_left() def contact(): hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def harvest_up(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.pick_beeper() def harvest(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() contact() turn_right() for i in range (2): harvest_down() harvest_up() harvest_down() harvest()
17
39
variable
Harvest up diagonally up and harvest down diagonally down There is also a change of direction in the code, so I used the harvest code at the end
cs1qa
null
null
null
null
null
Question: Could you briefly explain the harvest up and down functions? Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace("blue") #hubo.set_pause(1) def turn_right(): for i in range(3): hubo.turn_left() def contact(): hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def harvest_up(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.pick_beeper() def harvest(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() contact() turn_right() for i in range (2): harvest_down() harvest_up() harvest_down() harvest()
1
0
TA
What is turn_right()?
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() def zigzag_ready(): zigzag() hubo.turn_left() hubo.move() for i in range(4): zigzag_ready() zigzag()
9
11
variable
It is a function that rotates to the right, but it does not have a function to rotate to the right, so it is a function made with three left turns!
cs1qa
null
null
null
null
null
Question: What is turn_right()? 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() def zigzag_ready(): zigzag() hubo.turn_left() hubo.move() for i in range(4): zigzag_ready() zigzag()
5
0
TA
Task 1: Explain what each of the global variables a, b, c is!
from cs1robots import * #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() a=1 b=0 c=0 def read(): global a,b,c while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() c=c+1 b=b+c*a c=0 a=a*10 hubo.move() a=1 def write(x): while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): for i in range(x%10): hubo.drop_beeper() x=int(x/10) hubo.move() read() turn_right() hubo.move() turn_right() read() hubo.turn_left() hubo.move() hubo.turn_left() write(b)
11
13
variable
a is the exponent of 10 (1 digit 1, 10 digit 10), c is each digit, b is the total
cs1qa
null
null
null
null
null
Question: Task 1: Explain what each of the global variables a, b, c is! Code: from cs1robots import * #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') hubo = Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() a=1 b=0 c=0 def read(): global a,b,c while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): while hubo.on_beeper(): hubo.pick_beeper() c=c+1 b=b+c*a c=0 a=a*10 hubo.move() a=1 def write(x): while hubo.front_is_clear(): hubo.move() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): for i in range(x%10): hubo.drop_beeper() x=int(x/10) hubo.move() read() turn_right() hubo.move() turn_right() read() hubo.turn_left() hubo.move() hubo.turn_left() write(b)
8
0
TA
How did you create the Task1 merge() function?
from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # .. g=open(output_filename,'w') for i in input_filenames: f=open(i,'r') for lines in f.readlines(): g.write(lines) ##g.write("\n") ##g.write("\n") f.close() g.close() 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')
2
13
code_explain
While reading the input filename with the for statement, all opened files are read and written to the output file.
cs1qa
null
null
null
null
null
Question: How did you create the Task1 merge() function? Code: from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here # .. g=open(output_filename,'w') for i in input_filenames: f=open(i,'r') for lines in f.readlines(): g.write(lines) ##g.write("\n") ##g.write("\n") f.close() g.close() 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')
6
2
TA
How did you implement showing all the pictures at the beginning
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 == num2 or num1 < 0 or num2 < 0 or num1 >= 24 or num2 >= 24 or num1 in correct_list or num2 in correct_list): 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.pop() correct_list.pop() print_cards() return False else: return True return False initialize() for i in range(24): correct_list.append(i) print_cards() for i in range(24): correct_list.pop() 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
98
103
code_explain
Implemented in lines 99-104. First, I appended 1 to 24 to the correct_list and then called print_cards to show the image. Then, correct_list was cleared by doing correct_list.pop() 24 times. Another call to print_cards was made to show only the numbers this time.
cs1qa
null
null
null
null
null
Question: How did you implement showing all the pictures at the beginning 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 == num2 or num1 < 0 or num2 < 0 or num1 >= 24 or num2 >= 24 or num1 in correct_list or num2 in correct_list): 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.pop() correct_list.pop() print_cards() return False else: return True return False initialize() for i in range(24): correct_list.append(i) print_cards() for i in range(24): correct_list.pop() 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
8
0
TA
Please explain code 1
import elice_utils from time import sleep def merge(input_filenames, output_filename): # Implement here # ... input_file = [] s = "" for i in range(len(input_filenames)): input_file.append(open(input_filenames[i], "r" )) s = s + input_file[i].read() + "\n" for i in range(len(input_filenames)): input_file[i].close() output_file = open(output_filename,"w") output_file.write(s) 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
14
code_explain
I read three text files with the read function and made them into one string to create an output file.
cs1qa
null
null
null
null
null
Question: Please explain code 1 Code: import elice_utils from time import sleep def merge(input_filenames, output_filename): # Implement here # ... input_file = [] s = "" for i in range(len(input_filenames)): input_file.append(open(input_filenames[i], "r" )) s = s + input_file[i].read() + "\n" for i in range(len(input_filenames)): input_file[i].close() output_file = open(output_filename,"w") output_file.write(s) 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')
6
2
TA
Please explain the list called correct
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 correct[i]: # 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 not((0<=num1<=23 and 0<=num2<=23) and num1!=num2) or correct[num1] or correct[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. ########################################################################### correct[num1]=1 correct[num2]=1 print_cards() if cards[num1][1]==cards[num2][1]: return True else: correct[num1]=0 correct[num2]=0 print_cards() return False initialize() correct=[] for i in range(24): correct.append(1) print_cards() print("### Welcome to the Python Memento game!!! ###") for i in range(24): correct[i]=0 print_cards() ############################################################################### 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!") correct_list.append(num1) correct_list.append(num2) else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries+=1 ###########################################################################
114
117
variable
If the n number of correct is 1, the picture is printed at the card output stage, and if it is 0, the number card is printed at the card output stage.
cs1qa
null
null
null
null
null
Question: Please explain the list called correct 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 correct[i]: # 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 not((0<=num1<=23 and 0<=num2<=23) and num1!=num2) or correct[num1] or correct[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. ########################################################################### correct[num1]=1 correct[num2]=1 print_cards() if cards[num1][1]==cards[num2][1]: return True else: correct[num1]=0 correct[num2]=0 print_cards() return False initialize() correct=[] for i in range(24): correct.append(1) print_cards() print("### Welcome to the Python Memento game!!! ###") for i in range(24): correct[i]=0 print_cards() ############################################################################### 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!") correct_list.append(num1) correct_list.append(num2) else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries+=1 ###########################################################################
9
0
TA
What elements is the cards list in?
# Copy your "Memento" code from the task in Lab 6. 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 c=0 class Card: def __init__(self): self.state = False def photo(self,img,name): self.img = img self.name = name def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = Card() temp_tuple.photo(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 cards[i].state : # 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. if (not cards[num1].state) and (not cards[num2].state) and (not num1 == num2) and num1>-1 and num1<24 and num2>-1 and num2<24: return True ########################################################################### 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. 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 print_cards() ########################################################################### return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not c == 12: # 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(c) + " 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!") c = c+1 print_cards() else: print("Wrong!") print_cards() ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries +=1 ###########################################################################
31
36
variable
The cards list is a list of currently 24 cards.
cs1qa
null
null
null
null
null
Question: What elements is the cards list in? Code: # Copy your "Memento" code from the task in Lab 6. 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 c=0 class Card: def __init__(self): self.state = False def photo(self,img,name): self.img = img self.name = name def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) temp_tuple = Card() temp_tuple.photo(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 cards[i].state : # 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. if (not cards[num1].state) and (not cards[num2].state) and (not num1 == num2) and num1>-1 and num1<24 and num2>-1 and num2<24: return True ########################################################################### 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. 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 print_cards() ########################################################################### return False initialize() print_cards() print("### Welcome to the Python Memento game!!! ###") ############################################################################### while not c == 12: # 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(c) + " 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!") c = c+1 print_cards() else: print("Wrong!") print_cards() ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries +=1 ###########################################################################
1
3
TA
Please explain each function
from cs1robots import * load_world("worlds/harvest1.wld") robot = Robot() robot.set_trace("blue") robot.set_pause(.1) def turn_right(): for i in range(3): robot.turn_left() def one_cycle(): for j in range(5): robot.move() robot.pick_beeper() robot.turn_left() robot.move() robot.pick_beeper() robot.turn_left() for k in range(5): robot.move() robot.pick_beeper() def set_next_position(): turn_right() robot.move() robot.pick_beeper() turn_right() robot.move() robot.pick_beeper() for l in range(2): one_cycle() set_next_position() one_cycle()
9
25
variable
one_cycle() is a function used to pick up two lines of beeper, and set_next_position() is a function used to move to the next line.
cs1qa
null
null
null
null
null
Question: Please explain each function Code: from cs1robots import * load_world("worlds/harvest1.wld") robot = Robot() robot.set_trace("blue") robot.set_pause(.1) def turn_right(): for i in range(3): robot.turn_left() def one_cycle(): for j in range(5): robot.move() robot.pick_beeper() robot.turn_left() robot.move() robot.pick_beeper() robot.turn_left() for k in range(5): robot.move() robot.pick_beeper() def set_next_position(): turn_right() robot.move() robot.pick_beeper() turn_right() robot.move() robot.pick_beeper() for l in range(2): one_cycle() set_next_position() one_cycle()
10
0
TA
Would you please write in this chat room as much detail as possible about what animation you made and how you met each requirement in the description?
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,500,Point(250,150)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000,200,Point(250,100)) sky.setFillColor('skyblue') sky.setDepth(99) _scene.add(sky) 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) """ create_world() # define your objects, e.g. mario = Mario('blue', 'normal') class Leaf (object): def __init__(self,x,y) : leaf = Layer() leaf1 = Circle(30, Point(x-15,y-45)) leaf1.setFillColor('green') leaf1.setDepth(51) leaf1.setBorderColor('green') leaf.add(leaf1) leaf2 = Circle(30, Point(x+15,y-45)) leaf2.setFillColor('green') leaf2.setDepth(51) leaf2.setBorderColor('green') leaf.add(leaf2) leaf3 = Circle(30, Point(x,y-70)) leaf3.setFillColor('green') leaf3.setDepth(51) leaf3.setBorderColor('green') leaf.add(leaf3) self.layer = leaf _scene.add(self.layer) def shake(self) : self.layer.move(10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(10,0) class Pillar (object) : def __init__(self,x,y) : m_pillar = Rectangle(30,90, Point(x,y)) m_pillar.setFillColor('brown') m_pillar.setDepth(52) self.layer = m_pillar _scene.add(self.layer) class Apple (object) : def __init__(self,x,y) : apple = Circle(10, Point(x,y)) apple.setFillColor('red') apple.setDepth(50) self.layer = apple _scene.add(self.layer) def drop(self) : self.layer.move(-12,5) sleep (0.1) self.layer.move(7,5) sleep (0.1) def broke (self,x,y) : self.layer.scale(0.001) broken1 = Ellipse(10,20,Point(x-20,y)) broken1.setFillColor('yellow') broken1.setDepth(50) broken2 = Ellipse(10,20,Point(x,y)) broken2.setFillColor('yellow') broken2.setDepth(50) _scene.add(broken1) _scene.add(broken2) class Rain (object) : def __init__(self,x,y) : rain = Ellipse(10,15,Point(x,y)) rain.setFillColor('blue') rain.setDepth(49) self.layer = rain _scene.add(self.layer) def shake(self) : self.layer.move(10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(10,0) pillar = Pillar(300,200) leaf = Leaf(300,200) apple = Apple(300,135) for i in range (15) : sleep(0.2) for a in range (18) : rain = Rain(20*a+30*a, 0+40*i) if i >= 10 : leaf.shake() for i in range(10) : apple.drop() apple.broke(250,255) # write your animation scenario here
null
null
code_explain
The overall animation content is an animation in which the apple on the tree falls and crushes due to the rain falling and the tree shakes. So, first, the gel first expresses the rain, and the leaves of the tree move on the canvas and the apple falls and crushes.
cs1qa
null
null
null
null
null
Question: Would you please write in this chat room as much detail as possible about what animation you made and how you met each requirement in the description? 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,500,Point(250,150)) grass.setFillColor('green') grass.setDepth(100) _scene.add(grass) sky = Rectangle(1000,200,Point(250,100)) sky.setFillColor('skyblue') sky.setDepth(99) _scene.add(sky) 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) """ create_world() # define your objects, e.g. mario = Mario('blue', 'normal') class Leaf (object): def __init__(self,x,y) : leaf = Layer() leaf1 = Circle(30, Point(x-15,y-45)) leaf1.setFillColor('green') leaf1.setDepth(51) leaf1.setBorderColor('green') leaf.add(leaf1) leaf2 = Circle(30, Point(x+15,y-45)) leaf2.setFillColor('green') leaf2.setDepth(51) leaf2.setBorderColor('green') leaf.add(leaf2) leaf3 = Circle(30, Point(x,y-70)) leaf3.setFillColor('green') leaf3.setDepth(51) leaf3.setBorderColor('green') leaf.add(leaf3) self.layer = leaf _scene.add(self.layer) def shake(self) : self.layer.move(10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(10,0) class Pillar (object) : def __init__(self,x,y) : m_pillar = Rectangle(30,90, Point(x,y)) m_pillar.setFillColor('brown') m_pillar.setDepth(52) self.layer = m_pillar _scene.add(self.layer) class Apple (object) : def __init__(self,x,y) : apple = Circle(10, Point(x,y)) apple.setFillColor('red') apple.setDepth(50) self.layer = apple _scene.add(self.layer) def drop(self) : self.layer.move(-12,5) sleep (0.1) self.layer.move(7,5) sleep (0.1) def broke (self,x,y) : self.layer.scale(0.001) broken1 = Ellipse(10,20,Point(x-20,y)) broken1.setFillColor('yellow') broken1.setDepth(50) broken2 = Ellipse(10,20,Point(x,y)) broken2.setFillColor('yellow') broken2.setDepth(50) _scene.add(broken1) _scene.add(broken2) class Rain (object) : def __init__(self,x,y) : rain = Ellipse(10,15,Point(x,y)) rain.setFillColor('blue') rain.setDepth(49) self.layer = rain _scene.add(self.layer) def shake(self) : self.layer.move(10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(-10,0) sleep (0.2) self.layer.move(10,0) pillar = Pillar(300,200) leaf = Leaf(300,200) apple = Apple(300,135) for i in range (15) : sleep(0.2) for a in range (18) : rain = Rain(20*a+30*a, 0+40*i) if i >= 10 : leaf.shake() for i in range(10) : apple.drop() apple.broke(250,255) # write your animation scenario here
4
2
TA
What are the conditions for showing cards in print_card?
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 global 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 sequence = [] for i in range(24): sequence.append(i) random.shuffle(sequence) temp_cards = [] for i in range(24): temp_cards.insert(sequence[i], cards[i]) cards = temp_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. for value in correct_list: if num1 == value or num2 == value: return False if num1 == num2: return False if type(num1) != int or type(num2) != int: return False if not ((num1 <=23 and num1 >= 0) and (num2 <=23 and num2 >= 0)): return False return True ########################################################################### 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. ########################################################################### 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: # 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) if cards[num1][1] == cards[num2][1]: correct_list.extend((num1, num2)) return True canvas.clear() 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 time.sleep(1) print_cards() return False initialize() canvas.clear() 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 time.sleep(1) 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.") tries = tries + 1 ########################################################################### 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!") if len(correct_list) == 24: print("You cleared this game") break ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
54
54
code_explain
print_card() only shows cards in the correct answer list
cs1qa
null
null
null
null
null
Question: What are the conditions for showing cards in print_card? 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 global 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 sequence = [] for i in range(24): sequence.append(i) random.shuffle(sequence) temp_cards = [] for i in range(24): temp_cards.insert(sequence[i], cards[i]) cards = temp_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. for value in correct_list: if num1 == value or num2 == value: return False if num1 == num2: return False if type(num1) != int or type(num2) != int: return False if not ((num1 <=23 and num1 >= 0) and (num2 <=23 and num2 >= 0)): return False return True ########################################################################### 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. ########################################################################### 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: # 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) if cards[num1][1] == cards[num2][1]: correct_list.extend((num1, num2)) return True canvas.clear() 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 time.sleep(1) print_cards() return False initialize() canvas.clear() 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 time.sleep(1) 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.") tries = tries + 1 ########################################################################### 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!") if len(correct_list) == 24: print("You cleared this game") break ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
4
3
TA
Can you briefly explain why you changed line 10 to 40+40*sin(x) in Task 4?
import math sin = math.sin pi = math.pi #n = int(input()) for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = 40+40*sin(x) # Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
9
9
reasoning
If you don't do that, the value is too small and the graph doesn't come out. So I changed it to show it well!
cs1qa
null
null
null
null
null
Question: Can you briefly explain why you changed line 10 to 40+40*sin(x) in Task 4? Code: import math sin = math.sin pi = math.pi #n = int(input()) for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = 40+40*sin(x) # Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
3
1
TA
Is there a reason you used while to give coins?
from cs1robots import * # Your code must work for all world files below. load_world( "worlds/trash1.wld" ) # load_world( "worlds/trash2.wld" ) hubo = Robot() hubo.set_trace('red') def go(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): go() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() for i in range(3): hubo.turn_left() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left()
11
12
reasoning
Well, if there are several coins apart, it’s just to pick them up
cs1qa
null
null
null
null
null
Question: Is there a reason you used while to give coins? 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() hubo.set_trace('red') def go(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() while hubo.front_is_clear(): go() hubo.turn_left() hubo.turn_left() while hubo.front_is_clear(): hubo.move() for i in range(3): hubo.turn_left() hubo.move() while hubo.carries_beepers(): hubo.drop_beeper() hubo.turn_left() hubo.turn_left() hubo.move() hubo.turn_left()
5
1
TA
You defined a layer called crab in Task 2. What are the advantages of having a layer like this?
from cs1graphics import * from time import sleep r = Rectangle(800, 160) r.setFillColor("beige") c = Circle(50) c.setFillColor("yellow") crab = Layer() body = Rectangle(140, 60, Point(0, -20)) body.setFillColor("orange") crab.add(body) eye1 = Circle(10, Point(-40, -60)) eye1.setFillColor("black") crab.add(eye1) eye2 = Circle(10, Point(40, -60)) eye2.setFillColor("black") crab.add(eye2) wing1 = Polygon(Point(-70, -50), Point(-120, -80), Point(-90, -30)) wing1.setFillColor("red") crab.add(wing1) wing2 = Polygon(Point(70, -50), Point(120, -80), Point(90, -30)) wing2.setFillColor("red") crab.add(wing2) leg1 = Polygon(Point(-70, 10), Point(-100, 20), Point(-80, 60)) leg1.setFillColor("brown") crab.add(leg1) leg2 = Polygon(Point(70, 10), Point(100, 20), Point(80, 60)) leg2.setFillColor("brown") crab.add(leg2) def draw_animal(): # Implement this function. canvas = Canvas(800, 600) canvas.setBackgroundColor("skyblue") canvas.setTitle("My World") canvas.add(r) r.moveTo(400, 500) canvas.add(c) c.moveTo(700, 100) canvas.add(crab) crab.moveTo(120, 400) pass def show_animation(): # Implement this function. for i in range(280): crab.move(2, 0) sleep(0.01) wing1.rotate(40) wing2.rotate(320) sleep(0.1) pass draw_animal() show_animation()
9
9
reasoning
If you put a layer, you can move the shapes contained in that layer at once!
cs1qa
null
null
null
null
null
Question: You defined a layer called crab in Task 2. What are the advantages of having a layer like this? Code: from cs1graphics import * from time import sleep r = Rectangle(800, 160) r.setFillColor("beige") c = Circle(50) c.setFillColor("yellow") crab = Layer() body = Rectangle(140, 60, Point(0, -20)) body.setFillColor("orange") crab.add(body) eye1 = Circle(10, Point(-40, -60)) eye1.setFillColor("black") crab.add(eye1) eye2 = Circle(10, Point(40, -60)) eye2.setFillColor("black") crab.add(eye2) wing1 = Polygon(Point(-70, -50), Point(-120, -80), Point(-90, -30)) wing1.setFillColor("red") crab.add(wing1) wing2 = Polygon(Point(70, -50), Point(120, -80), Point(90, -30)) wing2.setFillColor("red") crab.add(wing2) leg1 = Polygon(Point(-70, 10), Point(-100, 20), Point(-80, 60)) leg1.setFillColor("brown") crab.add(leg1) leg2 = Polygon(Point(70, 10), Point(100, 20), Point(80, 60)) leg2.setFillColor("brown") crab.add(leg2) def draw_animal(): # Implement this function. canvas = Canvas(800, 600) canvas.setBackgroundColor("skyblue") canvas.setTitle("My World") canvas.add(r) r.moveTo(400, 500) canvas.add(c) c.moveTo(700, 100) canvas.add(crab) crab.moveTo(120, 400) pass def show_animation(): # Implement this function. for i in range(280): crab.move(2, 0) sleep(0.01) wing1.rotate(40) wing2.rotate(320) sleep(0.1) pass draw_animal() show_animation()
4
0
TA
Please tell me the name of the function you are adding in Task1!
from cs1robots import * cnt=0 def go(a): for i in range(a): slave.move() def turn(a): for i in range(a): slave.turn_left() def pick_all(): global cnt while(slave.on_beeper()): slave.pick_beeper() cnt+=1 def drop_all(): global cnt for i in range(cnt): slave.drop_beeper() cnt=0 def add(): if(slave.on_beeper): pick_all() turn(3) go(1) drop_all() turn(2) go(1) turn(3) def over_ten(): global cnt pick_all() if(cnt>=10): for i in range(cnt%10): slave.drop_beeper() go(1) for i in range(cnt//10): slave.drop_beeper() else: drop_all() go(1) cnt=0 #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') slave = Robot() #slave.set_trace('blue') turn(1) go(1) turn(3) for i in range(10): add() if(slave.front_is_clear()): go(1) turn(3) go(1) turn(3) while(slave.on_beeper()): over_ten()
34
46
variable
First, number 1 is over_ten, which simply sums up the numbers above and below and then implements decimal rounding that needs to be executed.
cs1qa
null
null
null
null
null
Question: Please tell me the name of the function you are adding in Task1! Code: from cs1robots import * cnt=0 def go(a): for i in range(a): slave.move() def turn(a): for i in range(a): slave.turn_left() def pick_all(): global cnt while(slave.on_beeper()): slave.pick_beeper() cnt+=1 def drop_all(): global cnt for i in range(cnt): slave.drop_beeper() cnt=0 def add(): if(slave.on_beeper): pick_all() turn(3) go(1) drop_all() turn(2) go(1) turn(3) def over_ten(): global cnt pick_all() if(cnt>=10): for i in range(cnt%10): slave.drop_beeper() go(1) for i in range(cnt//10): slave.drop_beeper() else: drop_all() go(1) cnt=0 #load_world('worlds/add1.wld') #load_world('worlds/add2.wld') load_world('worlds/add34.wld') slave = Robot() #slave.set_trace('blue') turn(1) go(1) turn(3) for i in range(10): add() if(slave.front_is_clear()): go(1) turn(3) go(1) turn(3) while(slave.on_beeper()): over_ten()
4
3
TA
Why do you add 40 to the end of line 9 in the 4th task?
import math sin = math.sin pi = math.pi cos = math.cos for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = math.sin(x)+40*sin(x)+40# Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
8
8
reasoning
I decided on the function, but I used trial and error a bit, but the first line was 40 #s.
cs1qa
null
null
null
null
null
Question: Why do you add 40 to the end of line 9 in the 4th task? Code: import math sin = math.sin pi = math.pi cos = math.cos for i in range(41) : x = float(i) / 40.0 * 2 * pi character_count_per_line = math.sin(x)+40*sin(x)+40# Change this line to print out sine curve correctly. output_str = '#' * int(character_count_per_line) print (output_str)
1
4
TA
Could you briefly explain the harvest up and down functions?
from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace("blue") #hubo.set_pause(1) def turn_right(): for i in range(3): hubo.turn_left() def contact(): hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def harvest_up(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.pick_beeper() def harvest(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() contact() turn_right() for i in range (2): harvest_down() harvest_up() harvest_down() harvest()
17
39
variable
Harvest up diagonally up and harvest down diagonally down There is also a change of direction in the code, so I used the harvest code at the end
cs1qa
null
null
null
null
null
Question: Could you briefly explain the harvest up and down functions? Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo = Robot() hubo.set_trace("blue") #hubo.set_pause(1) def turn_right(): for i in range(3): hubo.turn_left() def contact(): hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() def harvest_down(): for i in range(5): hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() hubo.turn_left() hubo.move() def harvest_up(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() hubo.move() turn_right() hubo.move() hubo.pick_beeper() def harvest(): for i in range(5): hubo.pick_beeper() hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.pick_beeper() contact() turn_right() for i in range (2): harvest_down() harvest_up() harvest_down() harvest()
1
0
TA
I think you wrote the code separately for only the last half of the 4 laps. Can I ask why?
from cs1robots import * create_world() hubo=Robot() hubo.turn_left() hubo.set_trace('blue') def turn_right(): for i in range (3): hubo.turn_left() for i in range(4): for j in range(9): hubo.move() turn_right() hubo.move() turn_right() for k in range(9): hubo.move() hubo.turn_left() hubo.move() hubo.turn_left() for j in range(9): hubo.move() turn_right() hubo.move() turn_right() for k in range(9): hubo.move()
11
24
reasoning
Lastly, the path was shorter than the one in front, so if it moved like in the loop, it hit the wall and did it separately.
cs1qa
null
null
null
null
null
Question: I think you wrote the code separately for only the last half of the 4 laps. Can I ask why? Code: from cs1robots import * create_world() hubo=Robot() hubo.turn_left() hubo.set_trace('blue') def turn_right(): for i in range (3): hubo.turn_left() for i in range(4): for j in range(9): hubo.move() turn_right() hubo.move() turn_right() for k in range(9): hubo.move() hubo.turn_left() hubo.move() hubo.turn_left() for j in range(9): hubo.move() turn_right() hubo.move() turn_right() for k in range(9): hubo.move()
3
3
TA
How did you determine the last entrance?
from cs1robots import * import time # 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. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace("blue") # Now close all the windows in the house! def turn_right(): for i in range(3): my_robot.turn_left() def turn_around(): for i in range(2): my_robot.turn_left() def go(): while (my_robot.get_pos()!=(3,6)): if my_robot.left_is_clear(): my_robot.turn_left() my_robot.move() if my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() my_robot.turn_left() my_robot.move() elif my_robot.front_is_clear(): my_robot.move() else: turn_right() my_robot.move() my_robot.turn_left() my_robot.move() go() my_robot.turn_left()
18
18
code_explain
The last entrance was fixed by running the while statement only when the location was not reached.
cs1qa
null
null
null
null
null
Question: How did you determine the last entrance? Code: from cs1robots import * import time # 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. my_robot = Robot(beepers=100, avenue=2, street=6, orientation='E') my_robot.set_trace("blue") # Now close all the windows in the house! def turn_right(): for i in range(3): my_robot.turn_left() def turn_around(): for i in range(2): my_robot.turn_left() def go(): while (my_robot.get_pos()!=(3,6)): if my_robot.left_is_clear(): my_robot.turn_left() my_robot.move() if my_robot.left_is_clear(): turn_around() my_robot.move() my_robot.drop_beeper() my_robot.turn_left() my_robot.move() elif my_robot.front_is_clear(): my_robot.move() else: turn_right() my_robot.move() my_robot.turn_left() my_robot.move() go() my_robot.turn_left()
1
0
TA
You wrote well with the for statement in Task1, but is there any reason you couldn't put lines 29~34 below in the for statement?
from cs1robots import* create_world() a=Robot() def stride(): for i in range(9): a.move() def turn_right(): for i in range(3): a.turn_left() a.set_trace('blue') for i in range(4): a.turn_left() stride() turn_right() a.move() turn_right() stride() a.turn_left() a.move() a.turn_left() stride() turn_right() a.move() turn_right() stride()
18
33
reasoning
If I try to do 5 repetitions, I hit the wall in the last step and cut it.
cs1qa
null
null
null
null
null
Question: You wrote well with the for statement in Task1, but is there any reason you couldn't put lines 29~34 below in the for statement? Code: from cs1robots import* create_world() a=Robot() def stride(): for i in range(9): a.move() def turn_right(): for i in range(3): a.turn_left() a.set_trace('blue') for i in range(4): a.turn_left() stride() turn_right() a.move() turn_right() stride() a.turn_left() a.move() a.turn_left() stride() turn_right() a.move() turn_right() stride()
4
2
TA
Please explain along with the process which constants of the math module were used in Task3~
import math sin = math.sin pi = math.pi a = (input('How many steps? ')) for i in range(int(a)): x = float(i) / 30.0 * 2 * pi print (sin(x))
2
9
code_explain
3. Using sin and pi, the sin value is expressed up to the input step value.
cs1qa
null
null
null
null
null
Question: Please explain along with the process which constants of the math module were used in Task3~ Code: import math sin = math.sin pi = math.pi a = (input('How many steps? ')) for i in range(int(a)): x = float(i) / 30.0 * 2 * pi print (sin(x))
5
1
TA
Is there a reason that only the legs were made outside the function??
from cs1graphics import * from time import sleep import random def draw_animal(): # Implement this function. canvas.setBackgroundColor('white') body=Polygon(Point(170,200),Point(130,200),Point(120,270),Point(180,270)) body.setFillColor('light blue') nup.add(body) face=Ellipse(100,55,Point(150,200)) face.setFillColor('light blue') nup.add(face) eye1=Circle(11,Point(170,200)) eye1.setFillColor('white') nup.add(eye1) eye_in1=Circle(7,Point(170,200)) eye_in1.setFillColor('black') nup.add(eye_in1) eye2=Circle(11,Point(130,200)) eye2.setFillColor('white') eye_in2=Circle(7,Point(130,200)) eye_in2.setFillColor('black') nup.add(eye2) nup.add(eye_in2) arm_l=Polygon(Point(140,205),Point(135,200),Point(105,270),Point(110,275)) arm_l.setFillColor('light blue') hand_l=Circle(7,Point(107,272)) hand_l.setFillColor('light blue') arm_r=Polygon(Point(165,200),Point(160,205),Point(190,275),Point(195,270)) arm_r.setFillColor('light blue') hand_r=Circle(7,Point(193,272)) hand_r.setFillColor('light blue') left.add(arm_l) left.add(hand_l) right.add(arm_r) right.add(hand_r) canvas.add(left) canvas.add(right) canvas.add(leg_l) canvas.add(foot_l) canvas.add(leg_r) canvas.add(foot_r) canvas.add(nup) def show_animation(): # Implement this function. for i in range(100): r=random.randint(0,255) g=random.randint(0,255) b=random.randint(0,255) a=(r,g,b) canvas.setBackgroundColor(a) nup.move(2,0) left.move(26,0) right.move(3,0) leg_l.move(2,0) leg_r.move(2,0) foot_l.move(8,0) foot_r.move(-6,0) sleep(0.5) left.move(-24,0) right.move(-25,0) foot_l.move(-6,0) foot_r.move(8,0) sleep(0.5) right.move(24,0) nup = Layer() left=Layer() right=Layer() canvas=Canvas(300,320) leg_l=Rectangle(11,50,Point(140,285)) leg_l.setFillColor('light blue') foot_l=Rectangle(20,8,Point(135,310)) foot_l.setFillColor('light blue') leg_r=Rectangle(11,50,Point(160,285)) leg_r.setFillColor('light blue') foot_r=Rectangle(20,8,Point(165,310)) foot_r.setFillColor('light blue') draw_animal() show_animation()
53
86
reasoning
The feet and legs move separately, so it looks like making it inside or outside, so I just made it outside!
cs1qa
null
null
null
null
null
Question: Is there a reason that only the legs were made outside the function?? Code: from cs1graphics import * from time import sleep import random def draw_animal(): # Implement this function. canvas.setBackgroundColor('white') body=Polygon(Point(170,200),Point(130,200),Point(120,270),Point(180,270)) body.setFillColor('light blue') nup.add(body) face=Ellipse(100,55,Point(150,200)) face.setFillColor('light blue') nup.add(face) eye1=Circle(11,Point(170,200)) eye1.setFillColor('white') nup.add(eye1) eye_in1=Circle(7,Point(170,200)) eye_in1.setFillColor('black') nup.add(eye_in1) eye2=Circle(11,Point(130,200)) eye2.setFillColor('white') eye_in2=Circle(7,Point(130,200)) eye_in2.setFillColor('black') nup.add(eye2) nup.add(eye_in2) arm_l=Polygon(Point(140,205),Point(135,200),Point(105,270),Point(110,275)) arm_l.setFillColor('light blue') hand_l=Circle(7,Point(107,272)) hand_l.setFillColor('light blue') arm_r=Polygon(Point(165,200),Point(160,205),Point(190,275),Point(195,270)) arm_r.setFillColor('light blue') hand_r=Circle(7,Point(193,272)) hand_r.setFillColor('light blue') left.add(arm_l) left.add(hand_l) right.add(arm_r) right.add(hand_r) canvas.add(left) canvas.add(right) canvas.add(leg_l) canvas.add(foot_l) canvas.add(leg_r) canvas.add(foot_r) canvas.add(nup) def show_animation(): # Implement this function. for i in range(100): r=random.randint(0,255) g=random.randint(0,255) b=random.randint(0,255) a=(r,g,b) canvas.setBackgroundColor(a) nup.move(2,0) left.move(26,0) right.move(3,0) leg_l.move(2,0) leg_r.move(2,0) foot_l.move(8,0) foot_r.move(-6,0) sleep(0.5) left.move(-24,0) right.move(-25,0) foot_l.move(-6,0) foot_r.move(8,0) sleep(0.5) right.move(24,0) nup = Layer() left=Layer() right=Layer() canvas=Canvas(300,320) leg_l=Rectangle(11,50,Point(140,285)) leg_l.setFillColor('light blue') foot_l=Rectangle(20,8,Point(135,310)) foot_l.setFillColor('light blue') leg_r=Rectangle(11,50,Point(160,285)) leg_r.setFillColor('light blue') foot_r=Rectangle(20,8,Point(165,310)) foot_r.setFillColor('light blue') draw_animal() show_animation()
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 global balance # Add the money to the current balance balance = balance + int(money) print("You deposited "+ str(money) + " won") ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance # Withdraw the money from the current balance if int(money) > balance: print("You've withdrawn "+ str(money) +" won") print("But you only have "+ str(balance) + " won") else: balance = balance - money print("You've withdraw "+ str(money)+" won") ################# ### implement ### ################# # Do something on here ! ################# def bank() : # Input : (None) No Input # Output : (None) No Output global balance while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process == 'd': dep = int(input("How much do you want to deposit?")) deposit(dep) elif process == 'w': wit = int(input('How much do you want to withdraw?')) withdrawal(wit) elif process == 'c': print("Your current balance is "+str(balance)+" won") elif process == 'return' or process == '': return 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()
2
5
variable
Since the functions used in this task are to add, subtract, or check a value from the global variable balance, he said that each function would use the global variable balance.
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 global balance # Add the money to the current balance balance = balance + int(money) print("You deposited "+ str(money) + " won") ################# def withdrawal(money) : # Input : (Integer) The amount of money that a user wants to withdraw # Output : (None) No Output global balance # Withdraw the money from the current balance if int(money) > balance: print("You've withdrawn "+ str(money) +" won") print("But you only have "+ str(balance) + " won") else: balance = balance - money print("You've withdraw "+ str(money)+" won") ################# ### implement ### ################# # Do something on here ! ################# def bank() : # Input : (None) No Input # Output : (None) No Output global balance while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process == 'd': dep = int(input("How much do you want to deposit?")) deposit(dep) elif process == 'w': wit = int(input('How much do you want to withdraw?')) withdrawal(wit) elif process == 'c': print("Your current balance is "+str(balance)+" won") elif process == 'return' or process == '': return 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()
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()
18
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()
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()
1
4
TA
In task 5, the range was set to (5, 0, -2) in the for statement. Please explain how to write the path using this.
from cs1robots import * import time load_world('worlds/harvest2.wld') hubo = Robot(beepers=25) sleep_time = 0.1 def turn_right(): for i in range(3): hubo.turn_left() def pick(): if hubo.on_beeper(): time.sleep(sleep_time) hubo.pick_beeper() for i in range(5): hubo.move() for i in range(5, 0, -2): for j in range(i): hubo.turn_left() time.sleep(sleep_time) hubo.move() pick() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() for j in range(i): time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() time.sleep(sleep_time) hubo.move() turn_right() hubo.move() hubo.turn_left() for j in range(i): pick() time.sleep(sleep_time) hubo.move() hubo.turn_left() time.sleep(sleep_time) hubo.move() turn_right() hubo.turn_left() hubo.turn_left() for j in range(i-1): pick() time.sleep(sleep_time) hubo.move() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() hubo.move()
19
63
code_explain
Using the method of picking up beepers like a snail shell turns, I picked up the beeper while spinning while eating 5 for the first wheel and 3 for the second.
cs1qa
null
null
null
null
null
Question: In task 5, the range was set to (5, 0, -2) in the for statement. Please explain how to write the path using this. Code: from cs1robots import * import time load_world('worlds/harvest2.wld') hubo = Robot(beepers=25) sleep_time = 0.1 def turn_right(): for i in range(3): hubo.turn_left() def pick(): if hubo.on_beeper(): time.sleep(sleep_time) hubo.pick_beeper() for i in range(5): hubo.move() for i in range(5, 0, -2): for j in range(i): hubo.turn_left() time.sleep(sleep_time) hubo.move() pick() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() for j in range(i): time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() time.sleep(sleep_time) hubo.move() turn_right() hubo.move() hubo.turn_left() for j in range(i): pick() time.sleep(sleep_time) hubo.move() hubo.turn_left() time.sleep(sleep_time) hubo.move() turn_right() hubo.turn_left() hubo.turn_left() for j in range(i-1): pick() time.sleep(sleep_time) hubo.move() turn_right() time.sleep(sleep_time) hubo.move() hubo.turn_left() pick() hubo.move()
4
0
TA
Briefly explain what algorithm you wrote in Task 1!
from cs1robots import * import time load_world('worlds/add34.wld') hubo = Robot() #Defining functions def turn_right(): for i in range(3): hubo.turn_left() def turn_around(): for i in range(2): hubo.turn_left() def Sum(a, b): return a+b #Main algorithm number_of_beeper = 0 Number_in_row1 = 0 Number_in_row2 = 0 Sum = 0 hubo.turn_left() hubo.move() turn_right() for i in range(10): if hubo.on_beeper(): number_of_beeper = 0 x, y = hubo.get_pos() while hubo.on_beeper(): hubo.pick_beeper() number_of_beeper += 1 Number_in_row2 += (10**(10-x)) * number_of_beeper if hubo.front_is_clear(): hubo.move() turn_right() hubo.move() turn_right() for i in range(10): if hubo.on_beeper(): number_of_beeper = 0 x, y = hubo.get_pos() while hubo.on_beeper(): hubo.pick_beeper() number_of_beeper += 1 Number_in_row1 += (10**(10-x)) * number_of_beeper if hubo.front_is_clear(): hubo.move() turn_around() print(Number_in_row1, Number_in_row2) Sum = Number_in_row1 + Number_in_row2 print(Sum) a = int(Sum/1000) b = int((Sum-1000*a)/100) c = int((Sum-1000*a-100*b)/10) d = int(Sum-1000*a-100*b-10*c) print(a, b, c, d) for i in range(6): hubo.move() for i in range(a): hubo.drop_beeper() hubo.move() for i in range(b): hubo.drop_beeper() hubo.move() for i in range(c): hubo.drop_beeper() hubo.move() for i in range(d): hubo.drop_beeper() turn_around() for i in range(9): hubo.move()
20
77
code_explain
Pick up the beepers in row1 and row2, store the number represented by each row in Number_in_row1 and Number_in_row2, respectively, proceed with addition, and display the corresponding result as beeper in row1.
cs1qa
null
null
null
null
null
Question: Briefly explain what algorithm you wrote in Task 1! Code: from cs1robots import * import time load_world('worlds/add34.wld') hubo = Robot() #Defining functions def turn_right(): for i in range(3): hubo.turn_left() def turn_around(): for i in range(2): hubo.turn_left() def Sum(a, b): return a+b #Main algorithm number_of_beeper = 0 Number_in_row1 = 0 Number_in_row2 = 0 Sum = 0 hubo.turn_left() hubo.move() turn_right() for i in range(10): if hubo.on_beeper(): number_of_beeper = 0 x, y = hubo.get_pos() while hubo.on_beeper(): hubo.pick_beeper() number_of_beeper += 1 Number_in_row2 += (10**(10-x)) * number_of_beeper if hubo.front_is_clear(): hubo.move() turn_right() hubo.move() turn_right() for i in range(10): if hubo.on_beeper(): number_of_beeper = 0 x, y = hubo.get_pos() while hubo.on_beeper(): hubo.pick_beeper() number_of_beeper += 1 Number_in_row1 += (10**(10-x)) * number_of_beeper if hubo.front_is_clear(): hubo.move() turn_around() print(Number_in_row1, Number_in_row2) Sum = Number_in_row1 + Number_in_row2 print(Sum) a = int(Sum/1000) b = int((Sum-1000*a)/100) c = int((Sum-1000*a-100*b)/10) d = int(Sum-1000*a-100*b-10*c) print(a, b, c, d) for i in range(6): hubo.move() for i in range(a): hubo.drop_beeper() hubo.move() for i in range(b): hubo.drop_beeper() hubo.move() for i in range(c): hubo.drop_beeper() hubo.move() for i in range(d): hubo.drop_beeper() turn_around() for i in range(9): hubo.move()
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() hubo.set_trace('blue') hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def goStraight(): for i in range(5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() def turnAroundLeft(): if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def turnAroundRight(): if hubo.on_beeper(): hubo.pick_beeper() turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() turn_right() for i in range(6): goStraight() if i == 5: pass elif i%2 == 0: turnAroundLeft() else: turnAroundRight()
11
33
code_explain
First, in the straight section, the goStraight() function was used to pick up the beeper, and turnAroundRight() and turnAroundLeft() were used when turning to the right and turning to the left, respectively.
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() hubo.set_trace('blue') hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def goStraight(): for i in range(5): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() def turnAroundLeft(): if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def turnAroundRight(): if hubo.on_beeper(): hubo.pick_beeper() turn_right() hubo.move() if hubo.on_beeper(): hubo.pick_beeper() turn_right() for i in range(6): goStraight() if i == 5: pass elif i%2 == 0: turnAroundLeft() else: turnAroundRight()
1
3
TA
Why is the condition checked???
from cs1robots import * class Superbot(Robot): def turn_right(self): for _ in range(3): self.turn_left() def turn_around(self): for _ in range(2): self.turn_left() load_world("worlds/harvest1.wld") hubo=Superbot() while True: if hubo.on_beeper(): hubo.pick_beeper() if hubo._x == 7 and hubo._dir == 3: hubo.turn_left() hubo.move() hubo.turn_left() elif hubo._x == 2 and hubo._dir == 1: hubo.turn_right() hubo.move() hubo.turn_right() elif hubo.front_is_clear(): hubo.move() if hubo._beeper_bag == 36: break
19
26
reasoning
Since the x value does not change even after the robot rotates, the direction is also checked.
cs1qa
null
null
null
null
null
Question: Why is the condition checked??? Code: from cs1robots import * class Superbot(Robot): def turn_right(self): for _ in range(3): self.turn_left() def turn_around(self): for _ in range(2): self.turn_left() load_world("worlds/harvest1.wld") hubo=Superbot() while True: if hubo.on_beeper(): hubo.pick_beeper() if hubo._x == 7 and hubo._dir == 3: hubo.turn_left() hubo.move() hubo.turn_left() elif hubo._x == 2 and hubo._dir == 1: hubo.turn_right() hubo.move() hubo.turn_right() elif hubo.front_is_clear(): hubo.move() if hubo._beeper_bag == 36: break
6
0
TA
Okay, please explain why you used pop() at the end!
def fibonacci(upper_bound): pass A=[0,1] C = True while A[-1] < upper_bound: a= A[-1]+A[-2] A.append(a) A.pop() return A print(fibonacci(1000))
0
8
reasoning
Because while was used, a value of True is obtained from the while statement in the last term before the upper bound, and one term above the upper bound is additionally calculated, so the last value is removed through pop().
cs1qa
null
null
null
null
null
Question: Okay, please explain why you used pop() at the end! Code: def fibonacci(upper_bound): pass A=[0,1] C = True while A[-1] < upper_bound: a= A[-1]+A[-2] A.append(a) A.pop() return A print(fibonacci(1000))
5
0
TA
How did you use 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+=money ################# 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: balance-=money return True else : return False ################# 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=input("How much do you want to deposit? ") deposit(int(money)) print("You deposted "+money+" won") elif process=='w': money=input("How much do you want to withdraw? ") print("You've withdraw "+money+" won") if not withdrawal(int(money)): print("But you only have "+str(balance)+" won") elif process=='c': print("Your current balance is "+str(balance)+" won") elif process=='': break ################# bank()
0
32
variable
The reason balance is used as a global variable is for easy access within both functions.
cs1qa
null
null
null
null
null
Question: How did you use 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+=money ################# 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: balance-=money return True else : return False ################# 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=input("How much do you want to deposit? ") deposit(int(money)) print("You deposted "+money+" won") elif process=='w': money=input("How much do you want to withdraw? ") print("You've withdraw "+money+" won") if not withdrawal(int(money)): print("But you only have "+str(balance)+" won") elif process=='c': print("Your current balance is "+str(balance)+" won") elif process=='': break ################# bank()
2
2
TA
Could you please tell me why I set the conditions this way?
from cs1robots import * load_world('worlds/hurdles3.wld') hubo= Robot(beepers=100) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def jump_one_hurdle(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() while not hubo.on_beeper(): if hubo.front_is_clear(): hubo.move() else: jump_one_hurdle()
10
22
reasoning
The second task condition told me to stop if you step on the beeper. If you step on the beeper, the conditions below will not work. Before stepping on the beeper, the lower door will run and move over the hurdles
cs1qa
null
null
null
null
null
Question: Could you please tell me why I set the conditions this way? Code: from cs1robots import * load_world('worlds/hurdles3.wld') hubo= Robot(beepers=100) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def jump_one_hurdle(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() while not hubo.on_beeper(): if hubo.front_is_clear(): hubo.move() else: jump_one_hurdle()
2
0
TA
Can you explain the right_up_move() function?
import cs1robots as rob rob.load_world('worlds/harvest3.wld') hubo=rob.Robot() hubo.set_trace('blue') def harv_move(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def right_up_move(): for i in range(5): harv_move() right() harv_move() right() def left_up_move(): for i in range(5): harv_move() hubo.turn_left() harv_move() hubo.turn_left() harv_move() for i in range(6): if i==5: for i in range(5): harv_move() elif not i%2: left_up_move() else: right_up_move()
17
22
variable
right up move The robot turned to the right and made it a function that goes up one line.
cs1qa
null
null
null
null
null
Question: Can you explain the right_up_move() function? Code: import cs1robots as rob rob.load_world('worlds/harvest3.wld') hubo=rob.Robot() hubo.set_trace('blue') def harv_move(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def right_up_move(): for i in range(5): harv_move() right() harv_move() right() def left_up_move(): for i in range(5): harv_move() hubo.turn_left() harv_move() hubo.turn_left() harv_move() for i in range(6): if i==5: for i in range(5): harv_move() elif not i%2: left_up_move() else: right_up_move()
9
0
TA
Please explain the attributes added to the Card class in Task 1.
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 = [] class Card: pass def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) card = Card() card.image = img card.name = names[i] card.state = False #temp_tuple = (img, names[i]) cards.append(card) 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(cards[i].state == True): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].image.moveTo(i_w + w, i_h+h) canvas.add(cards[i].image) 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(cards[num1].state == True or cards[num2].state == True): return False else: if(num1 == num2): return False else: if(num1 < 0 or num1 > 23): return False elif(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. ########################################################################### print_cards() if(cards[num1].name == cards[num2].name): cards[num1].state = True cards[num2].state = True print_cards() return True else: cards[num1].state = True cards[num2].state = True print_cards() cards[num1].state = False cards[num2].state = False print_cards() return False initialize() ncor = 0 for i in range(24): cards[i].state = True print_cards() for i in range(24): cards[i].state = False 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(ncor) + " 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!") ncor = ncor + 1 else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries = tries + 1 ###########################################################################
21
29
variable
.image is used when printing an image file, and .name is used when comparing the two cards to see if they are the same as the file name. If the state is False, it is used to print the back side, and if the state is False, the front side is printed.
cs1qa
null
null
null
null
null
Question: Please explain the attributes added to the Card class in Task 1. 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 = [] class Card: pass def initialize(): # initialize cards for i in range(6): for k in range(4): img = Image(path+names[i]) card = Card() card.image = img card.name = names[i] card.state = False #temp_tuple = (img, names[i]) cards.append(card) 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(cards[i].state == True): # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i].image.moveTo(i_w + w, i_h+h) canvas.add(cards[i].image) 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(cards[num1].state == True or cards[num2].state == True): return False else: if(num1 == num2): return False else: if(num1 < 0 or num1 > 23): return False elif(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. ########################################################################### print_cards() if(cards[num1].name == cards[num2].name): cards[num1].state = True cards[num2].state = True print_cards() return True else: cards[num1].state = True cards[num2].state = True print_cards() cards[num1].state = False cards[num2].state = False print_cards() return False initialize() ncor = 0 for i in range(24): cards[i].state = True print_cards() for i in range(24): cards[i].state = False 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(ncor) + " 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!") ncor = ncor + 1 else: print("Wrong!") ########################################################################### # 3-2-5. Update number of tries (global variable, tries) tries = tries + 1 ###########################################################################
2
3
TA
Yes, then 4 is similar to most 1, but please explain why you used while instead of if
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 go_straight(): for i in range(5): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() def left_move(): hubo.turn_left() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def one_circuit(): go_straight() while hubo.on_beeper(): hubo.pick_beeper() turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn_right() go_straight() hubo.pick_beeper for i in range(6): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() left_move() for i in range(2): one_circuit() while hubo.on_beeper(): hubo.pick_beeper() left_move() go_straight() while hubo.on_beeper(): hubo.pick_beeper()
10
52
reasoning
If is executed only once, but continues to execute if the condition is met, so if there were multiple beepers, while could be used to get them all.
cs1qa
null
null
null
null
null
Question: Yes, then 4 is similar to most 1, but please explain why you used while instead of if 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 go_straight(): for i in range(5): while hubo.on_beeper(): hubo.pick_beeper() hubo.move() def left_move(): hubo.turn_left() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() hubo.turn_left() def one_circuit(): go_straight() while hubo.on_beeper(): hubo.pick_beeper() turn_right() hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn_right() go_straight() hubo.pick_beeper for i in range(6): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() left_move() for i in range(2): one_circuit() while hubo.on_beeper(): hubo.pick_beeper() left_move() go_straight() while hubo.on_beeper(): hubo.pick_beeper()
2
2
TA
Please explain each function in Task3 Hurdle
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') bot = Robot() bot.set_trace('blue') def turn_right(): bot.turn_left() bot.turn_left() bot.turn_left() def jump_one_hurdle(): if bot.front_is_clear(): bot.move() else: bot.turn_left() bot.move() turn_right() bot.move() turn_right() bot.move() bot.turn_left() while not bot.on_beeper(): jump_one_hurdle() bot.pick_beeper()
14
33
variable
I made a function that rotates to the right, and after making more than one hurdle, when it reaches the beeper, it stops and picks up the beeper.
cs1qa
null
null
null
null
null
Question: Please explain each function in Task3 Hurdle 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') bot = Robot() bot.set_trace('blue') def turn_right(): bot.turn_left() bot.turn_left() bot.turn_left() def jump_one_hurdle(): if bot.front_is_clear(): bot.move() else: bot.turn_left() bot.move() turn_right() bot.move() turn_right() bot.move() bot.turn_left() while not bot.on_beeper(): jump_one_hurdle() bot.pick_beeper()
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()
6
0
TA
Task 1 I'll give you a brief explanation!
def fibonacci(upper_bound): numbers=[0, 1] n = 0 m = 0 while True: n = numbers[m] + numbers[m+1] if n > upper_bound: break numbers.append(n) m = m+1 return numbers pass print(fibonacci(1000))
4
9
code_explain
task1 uses the definition of the Fibonacci sequence as it is, and is configured to repeat the expression until the value of the sequence is greater than the upper bound.
cs1qa
null
null
null
null
null
Question: Task 1 I'll give you a brief explanation! Code: def fibonacci(upper_bound): numbers=[0, 1] n = 0 m = 0 while True: n = numbers[m] + numbers[m+1] if n > upper_bound: break numbers.append(n) m = m+1 return numbers pass print(fibonacci(1000))
6
2
TA
What are the conditions for showing the card?
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 start_time = time.time() for i in range(len(num_pads)): ################################################################ if True: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h + h) canvas.add(cards[i][0]) w = w+ 100 if w % 600 == 0: w = 0 h += 130 time.sleep(5) w = 0 h = 0 for i in range(len(num_pads)): 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 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 > 24 or num1 > 24 or num2 > 24 or num2 > 24: 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 canvas.add(cards[num1][0]) cards[num1][0].moveTo(i_w + 100 * (num1%6), i_h + 130 * (num1//6) ) canvas.add(cards[num2][0]) cards[num2][0].moveTo(i_w + 100 * (num2%6), i_h + 130 * (num2//6)) time.sleep(1) if cards[num2][1]==cards[num1][1]: correct_list.append(num1) correct_list.append(num2) return True else: canvas.remove(cards[num1][0]) canvas.remove(cards[num2][0]) return False print_cards() 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 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 += 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
113
116
code_explain
In is valid, I checked whether the range of integer numbers is correct, whether the two numbers are different, etc., and by checking in the check function, I checked whether the name in the temp tuple of the card is the same.
cs1qa
null
null
null
null
null
Question: What are the conditions for showing the card? 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 start_time = time.time() for i in range(len(num_pads)): ################################################################ if True: # 3-2-2. rewrite the condition for visualization. ################################################################ cards[i][0].moveTo(i_w + w, i_h + h) canvas.add(cards[i][0]) w = w+ 100 if w % 600 == 0: w = 0 h += 130 time.sleep(5) w = 0 h = 0 for i in range(len(num_pads)): 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 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 > 24 or num1 > 24 or num2 > 24 or num2 > 24: 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 canvas.add(cards[num1][0]) cards[num1][0].moveTo(i_w + 100 * (num1%6), i_h + 130 * (num1//6) ) canvas.add(cards[num2][0]) cards[num2][0].moveTo(i_w + 100 * (num2%6), i_h + 130 * (num2//6)) time.sleep(1) if cards[num2][1]==cards[num1][1]: correct_list.append(num1) correct_list.append(num2) return True else: canvas.remove(cards[num1][0]) canvas.remove(cards[num2][0]) return False print_cards() 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 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 += 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
6
2
student
Why does!
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 and num2 in correct_list: return False elif num1 == num2: 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 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) ###########################################################################
93
93
code_understanding
Because the while statement has to run until correct_list does not contain all the numbers I made it not equal
cs1qa
null
null
null
null
null
Question: Why does! 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 and num2 in correct_list: return False elif num1 == num2: 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 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) ###########################################################################
2
0
TA
Can you explain the right_up_move() function?
import cs1robots as rob rob.load_world('worlds/harvest3.wld') hubo=rob.Robot() hubo.set_trace('blue') def harv_move(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def right_up_move(): for i in range(5): harv_move() right() harv_move() right() def left_up_move(): for i in range(5): harv_move() hubo.turn_left() harv_move() hubo.turn_left() harv_move() for i in range(6): if i==5: for i in range(5): harv_move() elif not i%2: left_up_move() else: right_up_move()
17
22
variable
right up move The robot turned to the right and made it a function that goes up one line.
cs1qa
null
null
null
null
null
Question: Can you explain the right_up_move() function? Code: import cs1robots as rob rob.load_world('worlds/harvest3.wld') hubo=rob.Robot() hubo.set_trace('blue') def harv_move(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def right_up_move(): for i in range(5): harv_move() right() harv_move() right() def left_up_move(): for i in range(5): harv_move() hubo.turn_left() harv_move() hubo.turn_left() harv_move() for i in range(6): if i==5: for i in range(5): harv_move() elif not i%2: left_up_move() else: right_up_move()
3
2
TA
What is the meaning of the turn variable used here?
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("red") # hubo.set_pause(1) turn = 0 def turn_right(): for i in range(3): hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn = 0 while True: move() turn = turn + 1 print(turn) if not hubo.front_is_clear(): if (turn%2 == 0): if hubo.right_is_clear(): turn_right() hubo.move() turn_right() while hubo.on_beeper(): hubo.pick_beeper() else: break else: if hubo.left_is_clear(): hubo.turn_left() hubo.move() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() else: break while not hubo.facing_north(): turn_right() hubo.turn_left() move() hubo.turn_left() move() hubo.turn_left()
22
47
variable
When the turn is even number, it turns to the right, and when the turn is odd number, it turns to the left.
cs1qa
null
null
null
null
null
Question: What is the meaning of the turn variable used here? 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("red") # hubo.set_pause(1) turn = 0 def turn_right(): for i in range(3): hubo.turn_left() def move(): while hubo.front_is_clear(): hubo.move() while hubo.on_beeper(): hubo.pick_beeper() turn = 0 while True: move() turn = turn + 1 print(turn) if not hubo.front_is_clear(): if (turn%2 == 0): if hubo.right_is_clear(): turn_right() hubo.move() turn_right() while hubo.on_beeper(): hubo.pick_beeper() else: break else: if hubo.left_is_clear(): hubo.turn_left() hubo.move() hubo.turn_left() while hubo.on_beeper(): hubo.pick_beeper() else: break while not hubo.facing_north(): turn_right() hubo.turn_left() move() hubo.turn_left() move() hubo.turn_left()
6
2
TA
What do you do in the initialize function
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(): global cards # 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(): global 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 not in range(0, 24) and num2 not in range(0, 24): 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. ########################################################################### global correct_list temp_list = correct_list[:] correct_list.extend([num1, num2]) print_cards() if cards[num1][1] == cards[num2][1]: return True else: correct_list = temp_list print_cards() return False print("### Welcome to the Python Memento game!!! ###") initialize() correct_list = list(range(len(num_pads))) print_cards() time.sleep(3) correct_list = [] print_cards() ############################################################################### 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 % 100 in [11, 12, 13]: ordinal = "th" elif tries % 10 == 1: ordinal = "st" elif tries % 10 == 2: ordinal = "nd" elif tries % 10 == 3: ordinal = "rd" else: ordinal = "th" if tries == 1: pair = "pair" else: pair = "pairs" print(str(tries) + ordinal + " try. You got " + str(len(correct_list)//2) + " " + pair + ".") ########################################################################### 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
16
36
variable
I just added `random.shuffle(cards)` to shuffle the order of cards as what must be done.
cs1qa
null
null
null
null
null
Question: What do you do in the initialize function 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(): global cards # 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(): global 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 not in range(0, 24) and num2 not in range(0, 24): 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. ########################################################################### global correct_list temp_list = correct_list[:] correct_list.extend([num1, num2]) print_cards() if cards[num1][1] == cards[num2][1]: return True else: correct_list = temp_list print_cards() return False print("### Welcome to the Python Memento game!!! ###") initialize() correct_list = list(range(len(num_pads))) print_cards() time.sleep(3) correct_list = [] print_cards() ############################################################################### 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 % 100 in [11, 12, 13]: ordinal = "th" elif tries % 10 == 1: ordinal = "st" elif tries % 10 == 2: ordinal = "nd" elif tries % 10 == 3: ordinal = "rd" else: ordinal = "th" if tries == 1: pair = "pair" else: pair = "pairs" print(str(tries) + ordinal + " try. You got " + str(len(correct_list)//2) + " " + pair + ".") ########################################################################### 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
9
1
TA
In the second task, when the dealer's state is false, it checks, but why not check the player?
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: def __init__(self, suit, face, image, hidden = True): self.suit = suit self.face = face self.value = self.set_value(face) self.image = image self.state = hidden def set_value(self, face): if self.face.isdigit(): return int(self.face) elif self.face == "Ace": return 11 else: return 10 def create_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' """ deck = [] for suit in suit_names: for face in face_names: deck.append(Card(suit, face, Image(img_path+suit+'_'+face+".png"))) 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" """ tot_value = 0 for card in hand: tot_value += card.value return tot_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") """ article = 'a' if card.face == "Ace" or card.face == '8': article = "an" return "%s %s of %s" % (article, card.face, 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. """ while True: entered_str = input(prompt) if entered_str == 'y': return True elif entered_str == 'n': return False else: print("I beg your pardon!\n") 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_num in range(len(dealer)): card = dealer[card_num] if card.state == False: card = Card(card.suit, card.face, Image(img_path+"Back.png"), True) card.image.moveTo(x0+17*card_num, y0) card.image.setDepth(depth) bj_board.add(card.image) depth -= 10 for card_num in range(len(player)): card = player[card_num] card.image.moveTo(x1+17*card_num, y1) card.image.setDepth(depth) bj_board.add(card.image) depth -= 10 player_text = Text("Your Total : %d" % hand_value(player)) player_text.setFontColor("yellow") player_text.moveTo(500, 300) bj_board.add(player_text) if dealer[0].state: dealer_text = Text("The dealer's Total : %d" % hand_value(dealer)) dealer_text.setFontColor("yellow") dealer_text.moveTo(473, 100) bj_board.add(dealer_text) 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 ("\nYour 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()
126
130
reasoning
This is because the value of the state attribute will not be False for all cards the player has.
cs1qa
null
null
null
null
null
Question: In the second task, when the dealer's state is false, it checks, but why not check the player? 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: def __init__(self, suit, face, image, hidden = True): self.suit = suit self.face = face self.value = self.set_value(face) self.image = image self.state = hidden def set_value(self, face): if self.face.isdigit(): return int(self.face) elif self.face == "Ace": return 11 else: return 10 def create_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' """ deck = [] for suit in suit_names: for face in face_names: deck.append(Card(suit, face, Image(img_path+suit+'_'+face+".png"))) 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" """ tot_value = 0 for card in hand: tot_value += card.value return tot_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") """ article = 'a' if card.face == "Ace" or card.face == '8': article = "an" return "%s %s of %s" % (article, card.face, 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. """ while True: entered_str = input(prompt) if entered_str == 'y': return True elif entered_str == 'n': return False else: print("I beg your pardon!\n") 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_num in range(len(dealer)): card = dealer[card_num] if card.state == False: card = Card(card.suit, card.face, Image(img_path+"Back.png"), True) card.image.moveTo(x0+17*card_num, y0) card.image.setDepth(depth) bj_board.add(card.image) depth -= 10 for card_num in range(len(player)): card = player[card_num] card.image.moveTo(x1+17*card_num, y1) card.image.setDepth(depth) bj_board.add(card.image) depth -= 10 player_text = Text("Your Total : %d" % hand_value(player)) player_text.setFontColor("yellow") player_text.moveTo(500, 300) bj_board.add(player_text) if dealer[0].state: dealer_text = Text("The dealer's Total : %d" % hand_value(dealer)) dealer_text.setFontColor("yellow") dealer_text.moveTo(473, 100) bj_board.add(dealer_text) 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 ("\nYour 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()
4
0
TA
How did you implement the addition in step 1?
import cs1robots def main(): cs1robots.load_world("worlds/add34.wld") hubo = cs1robots.Robot(beepers=100) def turn_right(): for _ in range(3): hubo.turn_left() return None def back(): for _ in range(2): hubo.turn_left() return None def get(): cnt = 0 while(hubo.on_beeper()): cnt+=1 hubo.pick_beeper() return cnt def read(): sum = 0 while(True): sum = 10 * sum + get() if(not hubo.front_is_clear()): break hubo.move() back() while(hubo.front_is_clear()): hubo.move() return sum def drop(cnt): for _ in range(cnt): hubo.drop_beeper() def write(x): while(hubo.front_is_clear()): hubo.move() back() while(hubo.front_is_clear()): drop(x%10) x//=10 hubo.move() x=read() turn_right() hubo.move() turn_right() y=read() x += y hubo.turn_left() hubo.move() hubo.turn_left() write(x) main()
48
60
code_explain
First, we searched for the numbers in the same line through the read() function and found how many numbers were written in this line. I wrote a code that read() for line 1 and line 2, and then writes the sum of the two numbers in line 1 through write()
cs1qa
null
null
null
null
null
Question: How did you implement the addition in step 1? Code: import cs1robots def main(): cs1robots.load_world("worlds/add34.wld") hubo = cs1robots.Robot(beepers=100) def turn_right(): for _ in range(3): hubo.turn_left() return None def back(): for _ in range(2): hubo.turn_left() return None def get(): cnt = 0 while(hubo.on_beeper()): cnt+=1 hubo.pick_beeper() return cnt def read(): sum = 0 while(True): sum = 10 * sum + get() if(not hubo.front_is_clear()): break hubo.move() back() while(hubo.front_is_clear()): hubo.move() return sum def drop(cnt): for _ in range(cnt): hubo.drop_beeper() def write(x): while(hubo.front_is_clear()): hubo.move() back() while(hubo.front_is_clear()): drop(x%10) x//=10 hubo.move() x=read() turn_right() hubo.move() turn_right() y=read() x += y hubo.turn_left() hubo.move() hubo.turn_left() write(x) main()
4
0
TA
How do you get the number of beepers when writing?
import cs1robots def main(): cs1robots.load_world("worlds/add34.wld") hubo = cs1robots.Robot(beepers=100) def turn_right(): for _ in range(3): hubo.turn_left() return None def back(): for _ in range(2): hubo.turn_left() return None def get(): cnt = 0 while(hubo.on_beeper()): cnt+=1 hubo.pick_beeper() return cnt def read(): sum = 0 while(True): sum = 10 * sum + get() if(not hubo.front_is_clear()): break hubo.move() back() while(hubo.front_is_clear()): hubo.move() return sum def drop(cnt): for _ in range(cnt): hubo.drop_beeper() def write(x): while(hubo.front_is_clear()): hubo.move() back() while(hubo.front_is_clear()): drop(x%10) x//=10 hubo.move() x=read() turn_right() hubo.move() turn_right() y=read() x += y hubo.turn_left() hubo.move() hubo.turn_left() write(x) main()
39
46
code_explain
First, we moved the robot to the last column, then flipped the direction and moved forward, dividing the remainder of the number currently remaining in each column divided by 10, and then the remaining number divided by 10.
cs1qa
null
null
null
null
null
Question: How do you get the number of beepers when writing? Code: import cs1robots def main(): cs1robots.load_world("worlds/add34.wld") hubo = cs1robots.Robot(beepers=100) def turn_right(): for _ in range(3): hubo.turn_left() return None def back(): for _ in range(2): hubo.turn_left() return None def get(): cnt = 0 while(hubo.on_beeper()): cnt+=1 hubo.pick_beeper() return cnt def read(): sum = 0 while(True): sum = 10 * sum + get() if(not hubo.front_is_clear()): break hubo.move() back() while(hubo.front_is_clear()): hubo.move() return sum def drop(cnt): for _ in range(cnt): hubo.drop_beeper() def write(x): while(hubo.front_is_clear()): hubo.move() back() while(hubo.front_is_clear()): drop(x%10) x//=10 hubo.move() x=read() turn_right() hubo.move() turn_right() y=read() x += y hubo.turn_left() hubo.move() hubo.turn_left() write(x) main()
5
0
TA
What does bank() in task1 line 53 do?
balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance # Add the money to the current balance balance+= money print('You deposited '+ str(money) +' won') ################# ### 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 # Withdraw the money from the current balance if money > balance : print("You've withdrawn " + str(money)+ " won. " + "But you only have " + str(balance) + ' won') else: balance -= money print("You've withdrawn "+ str(money) +" won") ################# ### 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 == '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 == 'c': print('Your current balance is '+ str(balance) +' won') elif process == '': return False else: print('Please, press d or w or c or return') return bank() # 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()
52
52
variable
It plays the role of executing the function again after typing the word to enter the correct value
cs1qa
null
null
null
null
null
Question: What does bank() in task1 line 53 do? Code: balance = 0 def deposit(money) : # Input : (Integer) The amount of money that a user wants to deposit # Output : (None) No Output global balance # Add the money to the current balance balance+= money print('You deposited '+ str(money) +' won') ################# ### 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 # Withdraw the money from the current balance if money > balance : print("You've withdrawn " + str(money)+ " won. " + "But you only have " + str(balance) + ' won') else: balance -= money print("You've withdrawn "+ str(money) +" won") ################# ### 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 == '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 == 'c': print('Your current balance is '+ str(balance) +' won') elif process == '': return False else: print('Please, press d or w or c or return') return bank() # 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
Could you briefly explain what the one_cycle function of tasks 3 and 4 does?
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 turn_around() : for i in range(2) : hubo.turn_left() def one_cycle (): hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() def four_cycles() : for i in range(4) : one_cycle() four_cycles() hubo.move() hubo.drop_beeper() turn_around() hubo.move() four_cycles()
10
15
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/newspaper.wld') hubo=Robot(beepers = 1) hubo.set_trace("blue") def turn_right() : for i in range(3) : hubo.turn_left() def turn_around() : for i in range(2) : hubo.turn_left() def one_cycle (): hubo.move() hubo.turn_left() hubo.move() turn_right() hubo.move() def four_cycles() : for i in range(4) : one_cycle() four_cycles() hubo.move() hubo.drop_beeper() turn_around() hubo.move() four_cycles()
8
1
TA
For what reason did you put 5, 21, and 22 in task2?
name=[] location=[] f=open("average-latitude-longitude-countries.csv","r") lines=f.readlines() lines.pop(0) for line in lines: line=line.rstrip() line=line.split(",") if len(line)!=4: line[1]=line[1]+','+line[2] line.pop(2) line[0]=line[0].replace('"','') line[1]=line[1].replace('"','') country_code=str(line[0]) country_name=str(line[1]) latitude=float(line[2]) longitude=float(line[3]) name.append((country_code,country_name)) location.append((country_code,(latitude,longitude))) f.close() name.pop(0) location.pop(0) print(name) print(location) for i in range(len(location)): if location[i][1][1]<=0: print(name[i][1]) count_code=input("Enter country code: ") for i in range(len(name)): if name[i][0]==count_code: print(name[i][1]) break
4
21
reasoning
I removed the first line from 5, and it seems that there was a mistake in the correction process for 21 and 22
cs1qa
null
null
null
null
null
Question: For what reason did you put 5, 21, and 22 in task2? Code: name=[] location=[] f=open("average-latitude-longitude-countries.csv","r") lines=f.readlines() lines.pop(0) for line in lines: line=line.rstrip() line=line.split(",") if len(line)!=4: line[1]=line[1]+','+line[2] line.pop(2) line[0]=line[0].replace('"','') line[1]=line[1].replace('"','') country_code=str(line[0]) country_name=str(line[1]) latitude=float(line[2]) longitude=float(line[3]) name.append((country_code,country_name)) location.append((country_code,(latitude,longitude))) f.close() name.pop(0) location.pop(0) print(name) print(location) for i in range(len(location)): if location[i][1][1]<=0: print(name[i][1]) count_code=input("Enter country code: ") for i in range(len(name)): if name[i][0]==count_code: print(name[i][1]) break
2
4
TA
How is number used in this task?
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) # ... my_robot = Robot() my_robot.set_trace('blue') number=1 def turn_right(): for i in range (3): my_robot.turn_left() my_robot.turn_left() while True: if number%2==1: while my_robot.front_is_clear(): my_robot.move() if my_robot.right_is_clear(): turn_right() my_robot.move() turn_right() number=number+1 else: break else: while my_robot.front_is_clear(): my_robot.move() if my_robot.left_is_clear(): my_robot.turn_left() my_robot.move() my_robot.turn_left() number=number+1 else: break
22
42
variable
When the number is odd, the robot is moving upward, and when the number is even, the robot is moving downward.
cs1qa
null
null
null
null
null
Question: How is number used in this task? 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) # ... my_robot = Robot() my_robot.set_trace('blue') number=1 def turn_right(): for i in range (3): my_robot.turn_left() my_robot.turn_left() while True: if number%2==1: while my_robot.front_is_clear(): my_robot.move() if my_robot.right_is_clear(): turn_right() my_robot.move() turn_right() number=number+1 else: break else: while my_robot.front_is_clear(): my_robot.move() if my_robot.left_is_clear(): my_robot.turn_left() my_robot.move() my_robot.turn_left() number=number+1 else: break
9
0
TA
What are number and number_back functions?
# Copy your "Memento" code from the task in Lab 6. 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 = [] tries = 1 def suffle(): r1=random.randint(0,23) r2=random.randint(0,23) cards[r1], cards[r2]=cards[r2], cards[r1] ################################################### def is_valid(num1, num2): if num1>=0 and num1<=24 and num2>=0 and num2<=24: if not cards[num1].a: if not cards[num2].a: if num1!=num2: return True ########################################################################### # 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 def check(num1, num2): if cards[num1].name==cards[num2].name: return True ########################################################################### # 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. return False class Card: def __init__(self, i, Img, name): self.i=i self.Img=Img self.name=name self.Text = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) rect.setFillColor('white') self.Text.add(rect) self.Text.add(text) self.w = 0+i%6*100 self.h = 0+i//6*130 self.i_w = 70 self.i_h = 90 self.Text.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Text) self.a=True self.Text.setDepth(30) def number(self): self.Text.setDepth(20) self.a=False def number_back(self): self.Text.setDepth(30) self.a=True def img(self): self.Img.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Img) self.Img.setDepth(25) self.a=True cards=[] i=0 Pass=0 for j in range(6): for k in range(4): name=Card(i, Image(path+names[j]), names[j] ) cards.append(name) i+=1 for i in range(100): suffle() for i in range(24): cards[i]=Card(i, cards[i].Img, cards[i].name) for i in range(24): cards[i].img() time.sleep(1) for i in range(24): cards[i].number() 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(Pass) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue cards[num1].number_back() cards[num2].number_back() time.sleep(1) if check(num1, num2): print("Correct!") Pass+=1 else: print("Wrong!") cards[num1].number() cards[num2].number() tries+=1 if Pass==12: break
73
79
variable
task1 number function is a function that selects a number or a picture to be displayed on the screen by making the number above the picture and the back function making the picture above the number.
cs1qa
null
null
null
null
null
Question: What are number and number_back functions? Code: # Copy your "Memento" code from the task in Lab 6. 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 = [] tries = 1 def suffle(): r1=random.randint(0,23) r2=random.randint(0,23) cards[r1], cards[r2]=cards[r2], cards[r1] ################################################### def is_valid(num1, num2): if num1>=0 and num1<=24 and num2>=0 and num2<=24: if not cards[num1].a: if not cards[num2].a: if num1!=num2: return True ########################################################################### # 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 def check(num1, num2): if cards[num1].name==cards[num2].name: return True ########################################################################### # 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. return False class Card: def __init__(self, i, Img, name): self.i=i self.Img=Img self.name=name self.Text = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) rect.setFillColor('white') self.Text.add(rect) self.Text.add(text) self.w = 0+i%6*100 self.h = 0+i//6*130 self.i_w = 70 self.i_h = 90 self.Text.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Text) self.a=True self.Text.setDepth(30) def number(self): self.Text.setDepth(20) self.a=False def number_back(self): self.Text.setDepth(30) self.a=True def img(self): self.Img.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Img) self.Img.setDepth(25) self.a=True cards=[] i=0 Pass=0 for j in range(6): for k in range(4): name=Card(i, Image(path+names[j]), names[j] ) cards.append(name) i+=1 for i in range(100): suffle() for i in range(24): cards[i]=Card(i, cards[i].Img, cards[i].name) for i in range(24): cards[i].img() time.sleep(1) for i in range(24): cards[i].number() 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(Pass) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue cards[num1].number_back() cards[num2].number_back() time.sleep(1) if check(num1, num2): print("Correct!") Pass+=1 else: print("Wrong!") cards[num1].number() cards[num2].number() tries+=1 if Pass==12: break
6
1
TA
Please explain number 2 for each function
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] """ li=[] for i in range(trials): a = random.randrange(lb, ub+1) li.append(a) return li 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 """ n = len(num_list) a = 0 avg = num_list.copy() for i in range(n): a = a + avg.pop() b = a/n return b 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 """ count = [] copy = num_list.copy() copy.sort() a = copy.pop(0) b = copy.pop() for i in range(b-a+1): c = num_list.count(a+i) count.append((a+i, c)) return count # 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))
18
30
variable
average_integers() adds each term to the list created above and divides it by the length of the list to find the average.
cs1qa
null
null
null
null
null
Question: Please explain number 2 for each function 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] """ li=[] for i in range(trials): a = random.randrange(lb, ub+1) li.append(a) return li 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 """ n = len(num_list) a = 0 avg = num_list.copy() for i in range(n): a = a + avg.pop() b = a/n return b 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 """ count = [] copy = num_list.copy() copy.sort() a = copy.pop(0) b = copy.pop() for i in range(b-a+1): c = num_list.count(a+i) count.append((a+i, c)) return count # 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))
8
0
TA
How did you create the Task1 merge() function?
from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here a = open(input_filenames[0],"r") a1=a.read() b = open(input_filenames[1],"r") b1=b.read() c = open(input_filenames[2],"r") c1=c.read() d = open(output_filename, "w") d.write(a1+"\n"+b1+"\n"+c1) d.close() 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')
3
14
code_explain
The merge function of task 1 was written by reading the contents of each input file and appending the contents to the output file.
cs1qa
null
null
null
null
null
Question: How did you create the Task1 merge() function? Code: from time import sleep import elice_utils def merge(input_filenames, output_filename): # Implement here a = open(input_filenames[0],"r") a1=a.read() b = open(input_filenames[1],"r") b1=b.read() c = open(input_filenames[2],"r") c1=c.read() d = open(output_filename, "w") d.write(a1+"\n"+b1+"\n"+c1) d.close() 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')
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 # Add the money to the current balance global balance balance+=money 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 balance-=money def bank() : # Input : (None) No Input # Output : (None) No Output global balance while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return if process=='w': m=int(input("How much do you want to withdraw?")) if m>balance: print("You've withdrawn "+str(m)+' won') print('But you only have '+str(balance)+' won') else: withdrawal(m) print("You've withdrawn "+str(m)+' won') continue if process=='d': n=int(input("How much do you want to deposit?")) deposit(n) print("You deposited "+str(n)+'won') continue if process=='c': print("Your current balance is "+str(balance)+' won') continue 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. bank()
0
18
variable
Since the balance variable is a global variable, the value is changed by using the global function in the function.
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 # Add the money to the current balance global balance balance+=money 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 balance-=money def bank() : # Input : (None) No Input # Output : (None) No Output global balance while True: process = input("Deposit(d) or withdrawal(w) or balance check(c)? ") if process=='': return if process=='w': m=int(input("How much do you want to withdraw?")) if m>balance: print("You've withdrawn "+str(m)+' won') print('But you only have '+str(balance)+' won') else: withdrawal(m) print("You've withdrawn "+str(m)+' won') continue if process=='d': n=int(input("How much do you want to deposit?")) deposit(n) print("You deposited "+str(n)+'won') continue if process=='c': print("Your current balance is "+str(balance)+' won') continue 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. bank()
2
2
TA
What is the role of task3's while statement?
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(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() while not hubo.on_beeper(): if hubo.front_is_clear(): hubo.move() else: jump_one_hurdle()
23
27
code_explain
If the condition after while is satisfied, the following children are continuously executed. Task3 has to be executed before meeting the beeper, so I added not to the while to perform the function until the beeper is met.
cs1qa
null
null
null
null
null
Question: What is the role of task3's while statement? 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(): hubo.turn_left() hubo.move() turn_right() hubo.move() turn_right() hubo.move() hubo.turn_left() while not hubo.on_beeper(): if hubo.front_is_clear(): hubo.move() else: jump_one_hurdle()
6
0
TA
Please explain what you are doing inside the while statement!
def fibonacci(upper_bound): list = [0, 1] while (list[-1] + list[-2]) < upper_bound: list.append(list[-1]+list[-2]) return list print(fibonacci(1000))
2
3
code_explain
Add the last two terms of the list with Fibonacci numbers, and if less than the upper bound, add the sum of the two numbers to the list.
cs1qa
null
null
null
null
null
Question: Please explain what you are doing inside the while statement! Code: def fibonacci(upper_bound): list = [0, 1] while (list[-1] + list[-2]) < upper_bound: list.append(list[-1]+list[-2]) return list print(fibonacci(1000))
1
4
TA
Seeing that you created the harvest() function and called it several times at the end of the code, is there any reason you didn't use the for statement here?
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 move_next_beeper(): hubo.move() turn_right() hubo.move() hubo.turn_left() def harvest_one(): hubo.pick_beeper() move_next_beeper() def harvest(a): for i in range(2): for j in range(a): harvest_one() turn_right() hubo.turn_left() while not hubo.on_beeper(): hubo.move() for i in range(5): harvest_one() turn_right() harvest(5) harvest(4) harvest(3) harvest(2) harvest(1) hubo.pick_beeper()
15
30
reasoning
When I used the for statement, I couldn't use it because I didn't know how to change the number in ().
cs1qa
null
null
null
null
null
Question: Seeing that you created the harvest() function and called it several times at the end of the code, is there any reason you didn't use the for statement here? 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 move_next_beeper(): hubo.move() turn_right() hubo.move() hubo.turn_left() def harvest_one(): hubo.pick_beeper() move_next_beeper() def harvest(a): for i in range(2): for j in range(a): harvest_one() turn_right() hubo.turn_left() while not hubo.on_beeper(): hubo.move() for i in range(5): harvest_one() turn_right() harvest(5) harvest(4) harvest(3) harvest(2) harvest(1) hubo.pick_beeper()
2
0
TA
What is set2 function in task1?
from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def move_and_pick(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def set1(): if hubo.on_beeper(): hubo.pick_beeper() for i in range(5): move_and_pick() def set2(): set1() hubo.turn_left() move_and_pick() hubo.turn_left() set1() turn_right() hubo.move() set2() for i in range(2): hubo.move() turn_right() set2() hubo.turn_left()
23
29
variable
I created a function for two lines by combining two set1 functions that define the movement of a robot and a direction change function.
cs1qa
null
null
null
null
null
Question: What is set2 function in task1? Code: from cs1robots import * load_world('worlds/harvest3.wld') hubo = Robot() hubo.set_trace('blue') def move_and_pick(): hubo.move() if hubo.on_beeper(): hubo.pick_beeper() def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() def set1(): if hubo.on_beeper(): hubo.pick_beeper() for i in range(5): move_and_pick() def set2(): set1() hubo.turn_left() move_and_pick() hubo.turn_left() set1() turn_right() hubo.move() set2() for i in range(2): hubo.move() turn_right() set2() hubo.turn_left()
2
0
TA
What does Task 1's move_pick function do?
from cs1robots import* load_world("worlds/harvest3.wld") hubo = Robot() hubo.set_trace('blue') hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def move_pick(): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() else: hubo.move() def zig_zag(): for i in range(5): move_pick() hubo.turn_left() move_pick() hubo.turn_left() for i in range(5): move_pick() turn_right() move_pick() turn_right() for i in range(3): zig_zag()
8
13
variable
Where there is no beeper, it just goes away, and where there is a beeper, it picks up the beeper.
cs1qa
null
null
null
null
null
Question: What does Task 1's move_pick function do? Code: from cs1robots import* load_world("worlds/harvest3.wld") hubo = Robot() hubo.set_trace('blue') hubo.move() def turn_right(): for i in range(3): hubo.turn_left() def move_pick(): if hubo.on_beeper(): hubo.pick_beeper() hubo.move() else: hubo.move() def zig_zag(): for i in range(5): move_pick() hubo.turn_left() move_pick() hubo.turn_left() for i in range(5): move_pick() turn_right() move_pick() turn_right() for i in range(3): zig_zag()
4
2
TA
What are the termination conditions?
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 global 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 sequence = [] for i in range(24): sequence.append(i) random.shuffle(sequence) temp_cards = [] for i in range(24): temp_cards.insert(sequence[i], cards[i]) cards = temp_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. for value in correct_list: if num1 == value or num2 == value: return False if num1 == num2: return False if type(num1) != int or type(num2) != int: return False if not ((num1 <=23 and num1 >= 0) and (num2 <=23 and num2 >= 0)): return False return True ########################################################################### 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. ########################################################################### 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: # 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 correct_list.extend((num1, num2)) print_cards() time.sleep(1) if cards[num1][1] == cards[num2][1]: # correct_list.extend((num1, num2)) return True # canvas.clear() # 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 # time.sleep(1) del correct_list[-1] del correct_list[-1] print_cards() return False initialize() canvas.clear() 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 time.sleep(1) 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.") tries = tries + 1 ########################################################################### 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!") if len(correct_list) == 24: print("You cleared this game") break ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
181
183
code_explain
The end condition is when the length of the list of correct answers is 24.
cs1qa
null
null
null
null
null
Question: What are the termination conditions? 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 global 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 sequence = [] for i in range(24): sequence.append(i) random.shuffle(sequence) temp_cards = [] for i in range(24): temp_cards.insert(sequence[i], cards[i]) cards = temp_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. for value in correct_list: if num1 == value or num2 == value: return False if num1 == num2: return False if type(num1) != int or type(num2) != int: return False if not ((num1 <=23 and num1 >= 0) and (num2 <=23 and num2 >= 0)): return False return True ########################################################################### 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. ########################################################################### 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: # 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 correct_list.extend((num1, num2)) print_cards() time.sleep(1) if cards[num1][1] == cards[num2][1]: # correct_list.extend((num1, num2)) return True # canvas.clear() # 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 # time.sleep(1) del correct_list[-1] del correct_list[-1] print_cards() return False initialize() canvas.clear() 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 time.sleep(1) 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.") tries = tries + 1 ########################################################################### 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!") if len(correct_list) == 24: print("You cleared this game") break ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ###########################################################################
9
0
TA
What are number and number_back functions?
# Copy your "Memento" code from the task in Lab 6. 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 = [] tries = 1 def suffle(): r1=random.randint(0,23) r2=random.randint(0,23) cards[r1], cards[r2]=cards[r2], cards[r1] ################################################### def is_valid(num1, num2): if num1>=0 and num1<=24 and num2>=0 and num2<=24: if not cards[num1].a: if not cards[num2].a: if num1!=num2: return True ########################################################################### # 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 def check(num1, num2): if cards[num1].name==cards[num2].name: return True ########################################################################### # 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. return False class Card: def __init__(self, i, Img, name): self.i=i self.Img=Img self.name=name self.Text = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) rect.setFillColor('white') self.Text.add(rect) self.Text.add(text) self.w = 0+i%6*100 self.h = 0+i//6*130 self.i_w = 70 self.i_h = 90 self.Text.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Text) self.a=True self.Text.setDepth(30) def number(self): self.Text.setDepth(20) self.a=False def number_back(self): self.Text.setDepth(30) self.a=True def img(self): self.Img.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Img) self.Img.setDepth(25) self.a=True cards=[] i=0 Pass=0 for j in range(6): for k in range(4): name=Card(i, Image(path+names[j]), names[j] ) cards.append(name) i+=1 for i in range(100): suffle() for i in range(24): cards[i]=Card(i, cards[i].Img, cards[i].name) for i in range(24): cards[i].img() time.sleep(1) for i in range(24): cards[i].number() 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(Pass) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue cards[num1].number_back() cards[num2].number_back() time.sleep(1) if check(num1, num2): print("Correct!") Pass+=1 else: print("Wrong!") cards[num1].number() cards[num2].number() tries+=1 if Pass==12: break
73
77
variable
task1 number function is a function that selects a number or a picture to be displayed on the screen by making the number above the picture and the back function making the picture above the number.
cs1qa
null
null
null
null
null
Question: What are number and number_back functions? Code: # Copy your "Memento" code from the task in Lab 6. 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 = [] tries = 1 def suffle(): r1=random.randint(0,23) r2=random.randint(0,23) cards[r1], cards[r2]=cards[r2], cards[r1] ################################################### def is_valid(num1, num2): if num1>=0 and num1<=24 and num2>=0 and num2<=24: if not cards[num1].a: if not cards[num2].a: if num1!=num2: return True ########################################################################### # 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 def check(num1, num2): if cards[num1].name==cards[num2].name: return True ########################################################################### # 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. return False class Card: def __init__(self, i, Img, name): self.i=i self.Img=Img self.name=name self.Text = Layer() rect = Rectangle(90, 120, Point(0, 0)) text = Text(str(i), 18, Point(0, 0)) rect.setFillColor('white') self.Text.add(rect) self.Text.add(text) self.w = 0+i%6*100 self.h = 0+i//6*130 self.i_w = 70 self.i_h = 90 self.Text.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Text) self.a=True self.Text.setDepth(30) def number(self): self.Text.setDepth(20) self.a=False def number_back(self): self.Text.setDepth(30) self.a=True def img(self): self.Img.moveTo(self.i_w + self.w, self.i_h+self.h) canvas.add(self.Img) self.Img.setDepth(25) self.a=True cards=[] i=0 Pass=0 for j in range(6): for k in range(4): name=Card(i, Image(path+names[j]), names[j] ) cards.append(name) i+=1 for i in range(100): suffle() for i in range(24): cards[i]=Card(i, cards[i].Img, cards[i].name) for i in range(24): cards[i].img() time.sleep(1) for i in range(24): cards[i].number() 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(Pass) + " pairs.") ########################################################################### num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) if not is_valid(num1, num2): continue cards[num1].number_back() cards[num2].number_back() time.sleep(1) if check(num1, num2): print("Correct!") Pass+=1 else: print("Wrong!") cards[num1].number() cards[num2].number() tries+=1 if Pass==12: break
6
0
TA
Please explain what upper_bound does in Task 1
def fibonacci(upper_bound): a=0 b=1 c=0 result=[a,b] while a+b<=upper_bound: c=a+b result.append(c) a=b b=c return result print(fibonacci(1000))
5
5
variable
In task 1, upper_bound is used as a role to print a list of Fibonacci sequences until the value of a+b is less than upper_bound.
cs1qa
null
null
null
null
null
Question: Please explain what upper_bound does in Task 1 Code: def fibonacci(upper_bound): a=0 b=1 c=0 result=[a,b] while a+b<=upper_bound: c=a+b result.append(c) a=b b=c return result print(fibonacci(1000))
1
4
TA
Can you explain what task 5's t2 function does and how it was used?
from cs1robots import * load_world('worlds/harvest2.wld') hubo=Robot(beepers=1) hubo.set_trace('blue') def har(): for i in range(2): hubo.move() hubo.pick_beeper() def t1(x): hubo.move() for i in range(x): hubo.turn_left() hubo.move() hubo.pick_beeper() for i in range(x): hubo.turn_left() def t2(x): for i in range(x): hubo.turn_left() hubo.move() for i in range(x): hubo.turn_left() hubo.move() hubo.pick_beeper() def repeat(x): for i in range(x): har() hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() t1(3) repeat(1) t1(1) repeat(2) t1(3) repeat(3) t1(1) repeat(4) t1(3) repeat(5) t2(1) repeat(4) t2(3) repeat(3) t2(1) repeat(2) t2(3) repeat(1) t2(1)
16
50
variable
t2 is used when the robot passes through the right side (These are the functions used when the robot moves a straight path and then makes a turn)
cs1qa
null
null
null
null
null
Question: Can you explain what task 5's t2 function does and how it was used? Code: from cs1robots import * load_world('worlds/harvest2.wld') hubo=Robot(beepers=1) hubo.set_trace('blue') def har(): for i in range(2): hubo.move() hubo.pick_beeper() def t1(x): hubo.move() for i in range(x): hubo.turn_left() hubo.move() hubo.pick_beeper() for i in range(x): hubo.turn_left() def t2(x): for i in range(x): hubo.turn_left() hubo.move() for i in range(x): hubo.turn_left() hubo.move() hubo.pick_beeper() def repeat(x): for i in range(x): har() hubo.turn_left() for i in range(6): hubo.move() hubo.pick_beeper() t1(3) repeat(1) t1(1) repeat(2) t1(3) repeat(3) t1(1) repeat(4) t1(3) repeat(5) t2(1) repeat(4) t2(3) repeat(3) t2(1) repeat(2) t2(3) repeat(1) t2(1)
6
2
student
On top of return False in the check of the original code With print_cards() Does it mean to share the printouts in some cases?
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 = [] 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.suffle(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 == 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 int(num1) == int(num2): return False elif int(num1) > 23 or int(num2) > 23 : return False elif int(num1) < 0 or int(num2) < 0 : return False elif int(num1) in correct_list or int(num2) in correct_list : 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. ########################################################################### n1 = int(num1) n2 = int(num2) if cards[n1] == cards[n2] : correct_list.append(n1) correct_list.append(n2) return True 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) global tries tries = tries + 1 ###########################################################################
76
93
code_understanding
Yes, it seems like that, when it was wrong and when it was correct Because I have to show a different screen According to the conditions of showing photos in `print_cards` Separate cases appropriately and call `print_cards` for each.
cs1qa
null
null
null
null
null
Question: On top of return False in the check of the original code With print_cards() Does it mean to share the printouts in some cases? 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 = [] 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.suffle(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 == 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 int(num1) == int(num2): return False elif int(num1) > 23 or int(num2) > 23 : return False elif int(num1) < 0 or int(num2) < 0 : return False elif int(num1) in correct_list or int(num2) in correct_list : 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. ########################################################################### n1 = int(num1) n2 = int(num2) if cards[n1] == cards[n2] : correct_list.append(n1) correct_list.append(n2) return True 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) global tries tries = tries + 1 ###########################################################################
1
0
TA
if i<4: hubo.turn_left() hubo.move() Could you please explain why you added this part?
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() for i in range(5): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() if i<4: hubo.turn_left() hubo.move()
22
24
reasoning
If i=4, there is no more space left on the right, so I tried to stop the code
cs1qa
null
null
null
null
null
Question: if i<4: hubo.turn_left() hubo.move() Could you please explain why you added this part? 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() for i in range(5): hubo.turn_left() go_straight() turn_right() hubo.move() turn_right() go_straight() if i<4: hubo.turn_left() hubo.move()
1
0
TA
task 1 Please explain the worm function
from cs1robots import * create_world() hubo = Robot(beepers=10) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def worm(): for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(4): worm() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move()
8
18
variable
The worm function is a function that makes it the same as the initial state after going up and down once, going down again, going one space to the right.
cs1qa
null
null
null
null
null
Question: task 1 Please explain the worm function Code: from cs1robots import * create_world() hubo = Robot(beepers=10) hubo.set_trace('blue') def turn_right(): for i in range(3): hubo.turn_left() def worm(): for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move() hubo.turn_left() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(4): worm() for i in range(9): hubo.move() turn_right() hubo.move() turn_right() for i in range(9): hubo.move()
1
4
TA
Why did you define a separate function called pick()?
from cs1robots import * load_world('worlds/harvest2.wld') A=Robot() A.set_trace('blue') def turn_right() : A.turn_left() A.turn_left() A.turn_left() def right(): turn_right() A.move() turn_right() A.pick_beeper() A.move() def left(): A.turn_left() A.move() A.turn_left() A.pick_beeper() A.move() def pick() : A.move() A.pick_beeper() A.move() for i in range(5): A.move() A.turn_left() A.move() A.pick_beeper() A.turn_left() A.move() right() pick() left() pick() pick() right() for i in range(3): pick() left() for i in range(4): pick() right() for i in range(5): pick() A.turn_left() A.move() A.turn_left() A.move() for i in range(4): pick() A.move() A.pick_beeper() turn_right() A.move() turn_right() for i in range(3): pick() A.move() A.pick_beeper() A.turn_left() A.move() A.turn_left() for i in range(2): pick() A.move() A.pick_beeper() turn_right() A.move() turn_right() pick() A.move() A.pick_beeper() A.turn_left() A.move() A.turn_left() A.move() A.pick_beeper()
21
24
reasoning
As I was writing the code, I defined that behavior over and over again.
cs1qa
null
null
null
null
null
Question: Why did you define a separate function called pick()? Code: from cs1robots import * load_world('worlds/harvest2.wld') A=Robot() A.set_trace('blue') def turn_right() : A.turn_left() A.turn_left() A.turn_left() def right(): turn_right() A.move() turn_right() A.pick_beeper() A.move() def left(): A.turn_left() A.move() A.turn_left() A.pick_beeper() A.move() def pick() : A.move() A.pick_beeper() A.move() for i in range(5): A.move() A.turn_left() A.move() A.pick_beeper() A.turn_left() A.move() right() pick() left() pick() pick() right() for i in range(3): pick() left() for i in range(4): pick() right() for i in range(5): pick() A.turn_left() A.move() A.turn_left() A.move() for i in range(4): pick() A.move() A.pick_beeper() turn_right() A.move() turn_right() for i in range(3): pick() A.move() A.pick_beeper() A.turn_left() A.move() A.turn_left() for i in range(2): pick() A.move() A.pick_beeper() turn_right() A.move() turn_right() pick() A.move() A.pick_beeper() A.turn_left() A.move() A.turn_left() A.move() A.pick_beeper()
5
1
TA
Then in task2 Which two objects operate separately in animation?
from cs1graphics import * from time import sleep canvas=Canvas(800,800) canvas.setBackgroundColor('light blue') boat=Layer() row1=Rectangle(10,70,Point(60,0)) row2=Rectangle(10,70,Point(-20,0)) def draw_animal(): # Implement this function. body=Polygon(Point(-100,-60),Point(140,-60),Point(100,0),Point(-100,0)) body.setFillColor('darkgray') boat.add(body) pole=Rectangle(10,70,Point(20,-95)) pole.setFillColor('brown') flag=Rectangle(40,20,Point(0,-120)) flag.setFillColor('blue') boat.add(flag) boat.add(pole) row1.setFillColor('brown') row2.setFillColor('brown') boat.add(row1) boat.add(row2) boat.moveTo(100,700) canvas.add(boat) pass def show_animation(): # Implement this function. for i in range(90): boat.move(5,0) for j in range(45): row1.rotate(1) row2.rotate(1) sleep(0.01) row1.rotate(-45) row2.rotate(-45) sleep(0.001) pass draw_animal() show_animation()
28
36
code_explain
The whole boat and the rowing motion when the boat is moving were made to move separately
cs1qa
null
null
null
null
null
Question: Then in task2 Which two objects operate separately in animation? Code: from cs1graphics import * from time import sleep canvas=Canvas(800,800) canvas.setBackgroundColor('light blue') boat=Layer() row1=Rectangle(10,70,Point(60,0)) row2=Rectangle(10,70,Point(-20,0)) def draw_animal(): # Implement this function. body=Polygon(Point(-100,-60),Point(140,-60),Point(100,0),Point(-100,0)) body.setFillColor('darkgray') boat.add(body) pole=Rectangle(10,70,Point(20,-95)) pole.setFillColor('brown') flag=Rectangle(40,20,Point(0,-120)) flag.setFillColor('blue') boat.add(flag) boat.add(pole) row1.setFillColor('brown') row2.setFillColor('brown') boat.add(row1) boat.add(row2) boat.moveTo(100,700) canvas.add(boat) pass def show_animation(): # Implement this function. for i in range(90): boat.move(5,0) for j in range(45): row1.rotate(1) row2.rotate(1) sleep(0.01) row1.rotate(-45) row2.rotate(-45) sleep(0.001) pass draw_animal() show_animation()
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)
7
12
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)
2
4
TA
Please explain how you checked the termination condition of task5
from cs1robots import * # 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) # ... my_robot = Robot(beepers=1) my_robot.set_trace('blue') my_robot.set_pause(0.01) def turn_right(): for i in range(3): my_robot.turn_left() def turn_left_larger(): my_robot.turn_left() my_robot.move() my_robot.turn_left() def turn_right_larger(): turn_right() my_robot.move() turn_right() def go_straight(): while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): go_straight() if my_robot.right_is_clear(): turn_right_larger() else: my_robot.drop_beeper() go_straight() if my_robot.on_beeper(): pass elif my_robot.left_is_clear(): turn_left_larger() else: my_robot.drop_beeper() my_robot.pick_beeper()
36
49
code_explain
Originally, it would be good to solve the bias of avenues and streets, but it was not a form of entering the size, so I decided to use beeper. First, I thought of running the while statement by looking at go_straight/turn_right_larger/go_straight/turn_left_larger as a repeat unit. I made the beeper drop if the two functions didn't happen to prevent clogging in turn_right_larger and clogging in turn_left_larger depending on the avenues' dominance. Then, in the whole while statement, you can escape the while statement when it is blocked, so on_beeper is a condition, and when turn_right_larger is blocked, a pass part is written to exclude the case that turn_left_larger is executed immediately.
cs1qa
null
null
null
null
null
Question: Please explain how you checked the termination condition of task5 Code: from cs1robots import * # 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) # ... my_robot = Robot(beepers=1) my_robot.set_trace('blue') my_robot.set_pause(0.01) def turn_right(): for i in range(3): my_robot.turn_left() def turn_left_larger(): my_robot.turn_left() my_robot.move() my_robot.turn_left() def turn_right_larger(): turn_right() my_robot.move() turn_right() def go_straight(): while my_robot.front_is_clear(): my_robot.move() my_robot.turn_left() while not my_robot.on_beeper(): go_straight() if my_robot.right_is_clear(): turn_right_larger() else: my_robot.drop_beeper() go_straight() if my_robot.on_beeper(): pass elif my_robot.left_is_clear(): turn_left_larger() else: my_robot.drop_beeper() my_robot.pick_beeper()
6
2
TA
What do you do in the initialize function
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(): global cards # 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(): global 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 not in range(0, 24) and num2 not in range(0, 24): 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. ########################################################################### global correct_list temp_list = correct_list[:] correct_list.extend([num1, num2]) print_cards() if cards[num1][1] == cards[num2][1]: return True else: correct_list = temp_list print_cards() return False print("### Welcome to the Python Memento game!!! ###") initialize() correct_list = list(range(len(num_pads))) print_cards() time.sleep(3) correct_list = [] print_cards() ############################################################################### 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 % 100 in [11, 12, 13]: ordinal = "th" elif tries % 10 == 1: ordinal = "st" elif tries % 10 == 2: ordinal = "nd" elif tries % 10 == 3: ordinal = "rd" else: ordinal = "th" if tries == 1: pair = "pair" else: pair = "pairs" print(str(tries) + ordinal + " try. You got " + str(len(correct_list)//2) + " " + pair + ".") ########################################################################### 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
16
36
variable
I just added `random.shuffle(cards)` to shuffle the order of cards as what must be done.
cs1qa
null
null
null
null
null
Question: What do you do in the initialize function 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(): global cards # 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(): global 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 not in range(0, 24) and num2 not in range(0, 24): 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. ########################################################################### global correct_list temp_list = correct_list[:] correct_list.extend([num1, num2]) print_cards() if cards[num1][1] == cards[num2][1]: return True else: correct_list = temp_list print_cards() return False print("### Welcome to the Python Memento game!!! ###") initialize() correct_list = list(range(len(num_pads))) print_cards() time.sleep(3) correct_list = [] print_cards() ############################################################################### 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 % 100 in [11, 12, 13]: ordinal = "th" elif tries % 10 == 1: ordinal = "st" elif tries % 10 == 2: ordinal = "nd" elif tries % 10 == 3: ordinal = "rd" else: ordinal = "th" if tries == 1: pair = "pair" else: pair = "pairs" print(str(tries) + ordinal + " try. You got " + str(len(correct_list)//2) + " " + pair + ".") ########################################################################### 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
The last thing you did without diagonal_bottom() is because of the conflict?
from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() for i in range(4): hubo.move() def diagonal_top(): for i in range(6): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() turn_right() hubo.turn_left() hubo.move() hubo.turn_left() def diagonal_bottom(): for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.move() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(2): diagonal_top() diagonal_bottom() diagonal_top() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.pick_beeper()
37
45
reasoning
Yeah
cs1qa
null
null
null
null
null
Question: The last thing you did without diagonal_bottom() is because of the conflict? Code: from cs1robots import* load_world('worlds/harvest2.wld') hubo=Robot() hubo.set_trace('blue') def turn_right(): hubo.turn_left() hubo.turn_left() hubo.turn_left() for i in range(4): hubo.move() def diagonal_top(): for i in range(6): hubo.move() hubo.turn_left() hubo.move() hubo.pick_beeper() turn_right() hubo.turn_left() hubo.move() hubo.turn_left() def diagonal_bottom(): for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.pick_beeper() hubo.move() hubo.move() hubo.turn_left() hubo.turn_left() for i in range(2): diagonal_top() diagonal_bottom() diagonal_top() for i in range(5): hubo.move() hubo.pick_beeper() hubo.turn_left() hubo.move() turn_right() hubo.move() hubo.pick_beeper()
10
0
TA
What is boost() function?
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
That's a function where hulk grows by 1.1 times at intervals of t/5. I repeated it 10 times
cs1qa
null
null
null
null
null
Question: What is boost() function? 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()
3
0
TA
Please explain what you are doing in the left() function!
from cs1robots import * create_world() hubo=Robot(orientation='N', avenue=5, street=5) hubo.set_trace('blue') def left(): while not hubo.facing_north(): hubo.turn_left() hubo.turn_left() left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
5
8
variable
It's a function that makes you look left unconditionally
cs1qa
null
null
null
null
null
Question: Please explain what you are doing in the left() function! Code: from cs1robots import * create_world() hubo=Robot(orientation='N', avenue=5, street=5) hubo.set_trace('blue') def left(): while not hubo.facing_north(): hubo.turn_left() hubo.turn_left() left() while hubo.front_is_clear(): hubo.move() hubo.turn_left() while hubo.front_is_clear(): hubo.move() hubo.turn_left()
4
2
TA
What is x?
import math sin = math.sin pi = math.pi ''' for i in range(31): x = float(i) / 30.0 * 2 * pi print (sin(x)) ''' a = int(input('How many steps? ')) for i in range(a): x = float(i) / 30.0 * 2 * pi print (sin(x))
14
14
variable
float(i) / 30.0 * 2 * pi
cs1qa
null
null
null
null
null
Question: What is x? Code: import math sin = math.sin pi = math.pi ''' for i in range(31): x = float(i) / 30.0 * 2 * pi print (sin(x)) ''' a = int(input('How many steps? ')) for i in range(a): x = float(i) / 30.0 * 2 * pi print (sin(x))
6
2
TA
Where is the first part of initializing cards, and what is the type of each element in the cards??
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 in correct_list or i == num1 or i == 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 print_cards_initial(): canvas.clear() 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]) 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 correct_list.count(num1) != 0 or correct_list.count(num2) != 0 or num1 == num2 or num1 < 0 or num2 < 0 or num1 > 23 or num2 > 23: 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. ########################################################################### print_cards() if cards[num1] == cards[num2]: correct_list.append(num1) correct_list.append(num2) return True num1 = None num2 = None print_cards() return False initialize() print_cards_initial() 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 if tries == 1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") if tries == 2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") if tries == 3: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") if tries >= 4: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### global num1 num1 = int(input("Enter the first number: ")) global num2 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 ###########################################################################
16
34
variable
It seems that the first time cards are initialized in the initialize function, and the elements in cards are tuples.
cs1qa
null
null
null
null
null
Question: Where is the first part of initializing cards, and what is the type of each element in the cards?? 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 in correct_list or i == num1 or i == 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 print_cards_initial(): canvas.clear() 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]) 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 correct_list.count(num1) != 0 or correct_list.count(num2) != 0 or num1 == num2 or num1 < 0 or num2 < 0 or num1 > 23 or num2 > 23: 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. ########################################################################### print_cards() if cards[num1] == cards[num2]: correct_list.append(num1) correct_list.append(num2) return True num1 = None num2 = None print_cards() return False initialize() print_cards_initial() 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 if tries == 1: print(str(tries) + "st try. You got " + str(len(correct_list)//2) + " pairs.") if tries == 2: print(str(tries) + "nd try. You got " + str(len(correct_list)//2) + " pairs.") if tries == 3: print(str(tries) + "rd try. You got " + str(len(correct_list)//2) + " pairs.") if tries >= 4: print(str(tries) + "th try. You got " + str(len(correct_list)//2) + " pairs.") ########################################################################### global num1 num1 = int(input("Enter the first number: ")) global num2 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 ###########################################################################
9
0
student
so is the attribute a boolean?
# Copy your "Memento" code from the task in Lab 6. 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 = range(24) class Card: def __init__(self, n, i): self.name = n self.img = i def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card(names[i], Image(path + names[i])) # ncard.img = Image(path + names[i]) # ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) 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) ################################################################ # 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 in correct_list: # 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. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True 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) print_cards() if cards[num1].name == cards[num2].name: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) 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): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
18
21
code_understanding
That differs by your coding can be a boolean
cs1qa
null
null
null
null
null
Question: so is the attribute a boolean? Code: # Copy your "Memento" code from the task in Lab 6. 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 = range(24) class Card: def __init__(self, n, i): self.name = n self.img = i def initialize(): # initialize cards for i in range(6): for k in range(4): ncard = Card(names[i], Image(path + names[i])) # ncard.img = Image(path + names[i]) # ncard.name = names[i] # img = Image(path + names[i]) # temp_tuple = (img, names[i]) cards.append(ncard) 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) ################################################################ # 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 in correct_list: # 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. ########################################################################### if num1 != num2: if num2 not in correct_list: if num1 not in correct_list: if 0 <= num1 <= 23: if 0 <= num2 <= 23: return True 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) print_cards() if cards[num1].name == cards[num2].name: print_cards() return True correct_list.remove(num2) correct_list.remove(num1) print_cards() return False initialize() print_cards() time.sleep(3) 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): tries = tries + 1 continue if check(num1, num2): print("Correct!") else: print("Wrong!") tries = tries + 1 ########################################################################### # 3-2-5. Update number of tries (global variable, tries) ##################################################################
6
0
TA
Please explain what upper_bound does in Task 1
def fibonacci(upper_bound): result = [0, 1] index = 2 while True : if result[index - 2] + result[index - 1] < upper_bound : result.append(result[index - 2] + result[index - 1]) else : break index += 1 return result print(fibonacci(1000))
4
7
variable
Set the termination condition of the while loop
cs1qa
null
null
null
null
null
Question: Please explain what upper_bound does in Task 1 Code: def fibonacci(upper_bound): result = [0, 1] index = 2 while True : if result[index - 2] + result[index - 1] < upper_bound : result.append(result[index - 2] + result[index - 1]) else : break index += 1 return result print(fibonacci(1000))
1
3
TA
Could you please explain why 8-10 is so tight?
# 6 5 5 4 4 3 3 2 2 1 1 from cs1robots import * load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace('blue') for i in range(11): l = int(6-(i)/2) for _ in range(l): hubo.move() hubo.pick_beeper() hubo.turn_left()
7
12
reasoning
For convenience, I wanted to minimize the number of turns, so I made it move in a snail shape. When the length (number of movements) for one side was generalized, it was floor(6-i/2) so I wrote it like that
cs1qa
null
null
null
null
null
Question: Could you please explain why 8-10 is so tight? Code: # 6 5 5 4 4 3 3 2 2 1 1 from cs1robots import * load_world('worlds/harvest1.wld') hubo = Robot() hubo.set_trace('blue') for i in range(11): l = int(6-(i)/2) for _ in range(l): hubo.move() hubo.pick_beeper() hubo.turn_left()
8
2
TA
line 27 in task3 output.write(',') Why do I need it?
import elice_utils f=open("tpmon.txt","r") data=[] data2=[] counts=0 for line in f: data.append(line.strip()) count1=0 for i in range(1,len(data)): data2.append(1722+i) temp=data[i].split(" ") temp2=[] for i in range(len(temp)): if temp[i]!='': data2.append(float(temp[i])) print(data2) for i in range(int(len(data2)/13)): winter_avg=(data2[13*i+1]+data2[13*i+2])/2 summer_avg=(data2[13*i+7]+data2[13*i+8])/2 print('%d: %6.1f / %4.1f' % (data2[i*13], winter_avg, summer_avg)) output=open("tpmon.csv","w") for i in range(int(len(data2)/13)): for j in range(13): output.write(str(data2[13*i+j])) output.write(',') output.write('\n') elice_utils.send_file('tpmon.csv')
26
26
reasoning
In the problem, I asked for, in the output file, but since there is no, in the list, I put it.
cs1qa
null
null
null
null
null
Question: line 27 in task3 output.write(',') Why do I need it? Code: import elice_utils f=open("tpmon.txt","r") data=[] data2=[] counts=0 for line in f: data.append(line.strip()) count1=0 for i in range(1,len(data)): data2.append(1722+i) temp=data[i].split(" ") temp2=[] for i in range(len(temp)): if temp[i]!='': data2.append(float(temp[i])) print(data2) for i in range(int(len(data2)/13)): winter_avg=(data2[13*i+1]+data2[13*i+2])/2 summer_avg=(data2[13*i+7]+data2[13*i+8])/2 print('%d: %6.1f / %4.1f' % (data2[i*13], winter_avg, summer_avg)) output=open("tpmon.csv","w") for i in range(int(len(data2)/13)): for j in range(13): output.write(str(data2[13*i+j])) output.write(',') output.write('\n') elice_utils.send_file('tpmon.csv')
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()
0
126
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()
6
0
TA
Could you please explain what append does in Task 1?
def fibonacci(upper_bound): sequence=[0,1] n=0 while (sequence[n]+sequence[n+1])<1000: sequence.append(sequence[n]+sequence[n+1]) n+=1 return sequence print(fibonacci(1000))
0
6
variable
The append function is a function that adds an element to the end of the list. It serves to add a new Fibonacci number to the list to be returned.
cs1qa
null
null
null
null
null
Question: Could you please explain what append does in Task 1? Code: def fibonacci(upper_bound): sequence=[0,1] n=0 while (sequence[n]+sequence[n+1])<1000: sequence.append(sequence[n]+sequence[n+1]) n+=1 return sequence print(fibonacci(1000))
8
1
TA
Why is there an input("press again") in that animation?
from cs1graphics import * from time import sleep canvas = Canvas(800, 600) canvas.setBackgroundColor("light blue") canvas.setTitle("Zoo") ground = Rectangle(800, 250, Point(400, 475)) ground.setFillColor((0, 255, 0)) ground.setDepth(100) ground.setBorderWidth(5) ground.setBorderColor((0, 255, 0)) canvas.add(ground) elephant = Layer() leg1 = Layer() leg2 = Layer() leg3 = Layer() leg4 = Layer() head = Layer() tail = Layer() nose = Layer() elephant.add(leg1) elephant.add(leg2) elephant.add(leg3) elephant.add(leg4) elephant.add(head) elephant.add(tail) head.add(nose) body = Rectangle(140, 100, Point(400, 320)) body.setFillColor((170, 170, 170)) body.setBorderWidth(0) body.setDepth(100) elephant.add(body) face = Rectangle(80, 61, Point(320, 260)) face.setFillColor((170, 170, 170)) face.setBorderWidth(0) head.add(face) eye = Circle(10, Point(305, 255)) eye.setFillColor((30, 30, 30)) eye.setBorderWidth(0) head.add(eye) horn = Rectangle(60, 11, Point(250, 280)) horn.setFillColor((230, 230, 230)) horn.setBorderWidth(0) head.add(horn) ear_positionx = 90 ear_positiony = -50 ear1 = Rectangle(50, 60, Point(280+ear_positionx, 300+ear_positiony)) ear1.setFillColor((190, 190, 190)) ear1.setBorderWidth(0) ear1.setDepth(10) head.add(ear1) ear2 = Rectangle(20, 30, Point(270+ear_positionx, 290+ear_positiony)) ear2.setFillColor((200, 200, 200)) ear2.setBorderWidth(0) ear2.setDepth(5) head.add(ear2) nose1 = Rectangle(20, 30, Point(280, 300)) nose1.setFillColor((170, 170, 170)) nose1.setBorderWidth(7) nose1.setBorderColor((150, 150, 150)) nose.add(nose1) nose2 = Rectangle(20, 30, Point(276, 330)) nose2.setFillColor((170, 170, 170)) nose2.setBorderWidth(7) nose2.setBorderColor((150, 150, 150)) nose.add(nose2) nose3 = Rectangle(20, 30, Point(274, 360)) nose3.setFillColor((170, 170, 170)) nose3.setBorderWidth(7) nose3.setBorderColor((150, 150, 150)) nose.add(nose3) nose.move(0, -10) leg1_1 = Rectangle(40, 30, Point(280, 300)) leg1_1.setFillColor((170, 170, 170)) leg1_1.setBorderWidth(3) leg1_1.setBorderColor((150, 150, 150)) leg1.add(leg1_1) leg1_2 = Rectangle(40, 30, Point(276, 330)) leg1_2.setFillColor((170, 170, 170)) leg1_2.setBorderWidth(3) leg1_2.setBorderColor((150, 150, 150)) leg1.add(leg1_2) leg1.move(60, 80) leg2_1 = Rectangle(40, 30, Point(280, 300)) leg2_1.setFillColor((170, 170, 170)) leg2_1.setBorderWidth(3) leg2_1.setBorderColor((150, 150, 150)) leg2.add(leg2_1) leg2_2 = Rectangle(40, 30, Point(276, 330)) leg2_2.setFillColor((170, 170, 170)) leg2_2.setBorderWidth(3) leg2_2.setBorderColor((150, 150, 150)) leg2.add(leg2_2) leg2.move(180, 80) leg3_1 = Rectangle(40, 30, Point(280, 300)) leg3_1.setFillColor((170, 170, 170)) leg3_1.setBorderWidth(3) leg3_1.setBorderColor((150, 150, 150)) leg3.add(leg3_1) leg3_2 = Rectangle(40, 30, Point(276, 330)) leg3_2.setFillColor((170, 170, 170)) leg3_2.setBorderWidth(3) leg3_2.setBorderColor((150, 150, 150)) leg3.add(leg3_2) leg3.move(70, 75) leg3.setDepth(120) leg4_1 = Rectangle(40, 30, Point(280, 300)) leg4_1.setFillColor((170, 170, 170)) leg4_1.setBorderWidth(3) leg4_1.setBorderColor((150, 150, 150)) leg4.add(leg4_1) leg4_2 = Rectangle(40, 30, Point(276, 330)) leg4_2.setFillColor((170, 170, 170)) leg4_2.setBorderWidth(3) leg4_2.setBorderColor((150, 150, 150)) leg4.add(leg4_2) leg4.move(190, 75) leg4.setDepth(120) tail1 = Path(Point(465, 300), Point(500, 270),Point(550, 280), Point(590, 265)) tail1.setBorderWidth(10) tail1.setBorderColor((170, 170, 170)) tail.add(tail1) tail.setDepth(10) def draw_animal(): canvas.add(elephant) # Implement this function. pass def show_animation(): for i in range(10): elephant.move(-2, 0) leg1_1.move(1, 0) leg1_2.move(2, 0) leg2_1.move(1, 0) leg2_2.move(2, 0) leg3_1.move(-1, 0) leg3_2.move(-2, 0) leg4_1.move(-1, 0) leg4_2.move(-2, 0) input("press again") for i in range(20): for i in range(20): elephant.move(-2, 0) leg1_1.move(-1, 0) leg1_2.move(-2, 0) leg2_1.move(-1, 0) leg2_2.move(-2, 0) leg3_1.move(1, 0) leg3_2.move(2, 0) leg4_1.move(1, 0) leg4_2.move(2, 0) tail1.rotate(2) for i in range(20): elephant.move(-2, 0) leg1_1.move(1, 0) leg1_2.move(2, 0) leg2_1.move(1, 0) leg2_2.move(2, 0) leg3_1.move(-1, 0) leg3_2.move(-2, 0) leg4_1.move(-1, 0) leg4_2.move(-2, 0) tail1.rotate(-2) # Implement this function. pass draw_animal() input("press any key") elephant.move(300, 0) show_animation()
154
154
reasoning
I will fix it
cs1qa
null
null
null
null
null
Question: Why is there an input("press again") in that animation? Code: from cs1graphics import * from time import sleep canvas = Canvas(800, 600) canvas.setBackgroundColor("light blue") canvas.setTitle("Zoo") ground = Rectangle(800, 250, Point(400, 475)) ground.setFillColor((0, 255, 0)) ground.setDepth(100) ground.setBorderWidth(5) ground.setBorderColor((0, 255, 0)) canvas.add(ground) elephant = Layer() leg1 = Layer() leg2 = Layer() leg3 = Layer() leg4 = Layer() head = Layer() tail = Layer() nose = Layer() elephant.add(leg1) elephant.add(leg2) elephant.add(leg3) elephant.add(leg4) elephant.add(head) elephant.add(tail) head.add(nose) body = Rectangle(140, 100, Point(400, 320)) body.setFillColor((170, 170, 170)) body.setBorderWidth(0) body.setDepth(100) elephant.add(body) face = Rectangle(80, 61, Point(320, 260)) face.setFillColor((170, 170, 170)) face.setBorderWidth(0) head.add(face) eye = Circle(10, Point(305, 255)) eye.setFillColor((30, 30, 30)) eye.setBorderWidth(0) head.add(eye) horn = Rectangle(60, 11, Point(250, 280)) horn.setFillColor((230, 230, 230)) horn.setBorderWidth(0) head.add(horn) ear_positionx = 90 ear_positiony = -50 ear1 = Rectangle(50, 60, Point(280+ear_positionx, 300+ear_positiony)) ear1.setFillColor((190, 190, 190)) ear1.setBorderWidth(0) ear1.setDepth(10) head.add(ear1) ear2 = Rectangle(20, 30, Point(270+ear_positionx, 290+ear_positiony)) ear2.setFillColor((200, 200, 200)) ear2.setBorderWidth(0) ear2.setDepth(5) head.add(ear2) nose1 = Rectangle(20, 30, Point(280, 300)) nose1.setFillColor((170, 170, 170)) nose1.setBorderWidth(7) nose1.setBorderColor((150, 150, 150)) nose.add(nose1) nose2 = Rectangle(20, 30, Point(276, 330)) nose2.setFillColor((170, 170, 170)) nose2.setBorderWidth(7) nose2.setBorderColor((150, 150, 150)) nose.add(nose2) nose3 = Rectangle(20, 30, Point(274, 360)) nose3.setFillColor((170, 170, 170)) nose3.setBorderWidth(7) nose3.setBorderColor((150, 150, 150)) nose.add(nose3) nose.move(0, -10) leg1_1 = Rectangle(40, 30, Point(280, 300)) leg1_1.setFillColor((170, 170, 170)) leg1_1.setBorderWidth(3) leg1_1.setBorderColor((150, 150, 150)) leg1.add(leg1_1) leg1_2 = Rectangle(40, 30, Point(276, 330)) leg1_2.setFillColor((170, 170, 170)) leg1_2.setBorderWidth(3) leg1_2.setBorderColor((150, 150, 150)) leg1.add(leg1_2) leg1.move(60, 80) leg2_1 = Rectangle(40, 30, Point(280, 300)) leg2_1.setFillColor((170, 170, 170)) leg2_1.setBorderWidth(3) leg2_1.setBorderColor((150, 150, 150)) leg2.add(leg2_1) leg2_2 = Rectangle(40, 30, Point(276, 330)) leg2_2.setFillColor((170, 170, 170)) leg2_2.setBorderWidth(3) leg2_2.setBorderColor((150, 150, 150)) leg2.add(leg2_2) leg2.move(180, 80) leg3_1 = Rectangle(40, 30, Point(280, 300)) leg3_1.setFillColor((170, 170, 170)) leg3_1.setBorderWidth(3) leg3_1.setBorderColor((150, 150, 150)) leg3.add(leg3_1) leg3_2 = Rectangle(40, 30, Point(276, 330)) leg3_2.setFillColor((170, 170, 170)) leg3_2.setBorderWidth(3) leg3_2.setBorderColor((150, 150, 150)) leg3.add(leg3_2) leg3.move(70, 75) leg3.setDepth(120) leg4_1 = Rectangle(40, 30, Point(280, 300)) leg4_1.setFillColor((170, 170, 170)) leg4_1.setBorderWidth(3) leg4_1.setBorderColor((150, 150, 150)) leg4.add(leg4_1) leg4_2 = Rectangle(40, 30, Point(276, 330)) leg4_2.setFillColor((170, 170, 170)) leg4_2.setBorderWidth(3) leg4_2.setBorderColor((150, 150, 150)) leg4.add(leg4_2) leg4.move(190, 75) leg4.setDepth(120) tail1 = Path(Point(465, 300), Point(500, 270),Point(550, 280), Point(590, 265)) tail1.setBorderWidth(10) tail1.setBorderColor((170, 170, 170)) tail.add(tail1) tail.setDepth(10) def draw_animal(): canvas.add(elephant) # Implement this function. pass def show_animation(): for i in range(10): elephant.move(-2, 0) leg1_1.move(1, 0) leg1_2.move(2, 0) leg2_1.move(1, 0) leg2_2.move(2, 0) leg3_1.move(-1, 0) leg3_2.move(-2, 0) leg4_1.move(-1, 0) leg4_2.move(-2, 0) input("press again") for i in range(20): for i in range(20): elephant.move(-2, 0) leg1_1.move(-1, 0) leg1_2.move(-2, 0) leg2_1.move(-1, 0) leg2_2.move(-2, 0) leg3_1.move(1, 0) leg3_2.move(2, 0) leg4_1.move(1, 0) leg4_2.move(2, 0) tail1.rotate(2) for i in range(20): elephant.move(-2, 0) leg1_1.move(1, 0) leg1_2.move(2, 0) leg2_1.move(1, 0) leg2_2.move(2, 0) leg3_1.move(-1, 0) leg3_2.move(-2, 0) leg4_1.move(-1, 0) leg4_2.move(-2, 0) tail1.rotate(-2) # Implement this function. pass draw_animal() input("press any key") elephant.move(300, 0) show_animation()
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()
5
0
TA
Task 1. Please explain the role of the Withdrawal function.
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 ### ################# plus=int(input('How much you want to deposit')) print('you deposit %d won'%plus) global balance balance=money+plus ################# 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 ! minus=int(input('How much you want to withdraw')) if money>=minus: print('you withdraw %d won'%minus) global balance balance=money-minus else: print('you withdraw %d won'%minus) print('But you only have %d'%money) ################# 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': deposit(balance) elif process=='w': withdrawal(balance) elif process=='c': print('Your current balance is %d won'%balance) else: print('Please, press d or w or c or return') return False ################# ### implement ### ################# # Do something on here ! ################# bank()
17
35
variable
The withdrawal function receives the desired amount of deposit and compares it with the money in the bankbook, and prints a message stating that the amount of money remaining in the bankbook is larger and less, and the deposit is not made.
cs1qa
null
null
null
null
null
Question: Task 1. Please explain the role of the Withdrawal function. 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 ### ################# plus=int(input('How much you want to deposit')) print('you deposit %d won'%plus) global balance balance=money+plus ################# 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 ! minus=int(input('How much you want to withdraw')) if money>=minus: print('you withdraw %d won'%minus) global balance balance=money-minus else: print('you withdraw %d won'%minus) print('But you only have %d'%money) ################# 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': deposit(balance) elif process=='w': withdrawal(balance) elif process=='c': print('Your current balance is %d won'%balance) else: print('Please, press d or w or c or return') return False ################# ### implement ### ################# # Do something on here ! ################# bank()
6
0
TA
Please explain what upper_bound does in Task 1
def fibonacci(upper_bound): lista = [0, 1] a=0 b=0 while 1==1: b = lista[a] + lista[a+1] if (b < upper_bound): lista.append(b) else: break a=a+1 print(lista) pass print(fibonacci(1000))
0
11
variable
upper_bound refers to the upper limit of the Fibonacci number, and when the Fibonacci number in the code is larger than this, the loop is escaped.
cs1qa
null
null
null
null
null
Question: Please explain what upper_bound does in Task 1 Code: def fibonacci(upper_bound): lista = [0, 1] a=0 b=0 while 1==1: b = lista[a] + lista[a+1] if (b < upper_bound): lista.append(b) else: break a=a+1 print(lista) pass print(fibonacci(1000))