Day 6 - Beginner - Python Functions & Karel

時間:2021年2月7日 20:33~21:23
與會人員:Aileen, Sky, 玉米, 筑筑(依筆劃順序)
旁聽人員:Laurence, speedthunder, 胖胖(依筆劃順序)
導讀:Sky

本次改用新的討論方式,說明請見:

■ 6-56. Day 6 Goals: what we will make by the end of the day

三個主題:Code Blocks, Functions, While Loops

■ 6-57. Defining and Calling Python Functions

  1. defining functions
def my_function():
  # 縮排,要這放這裡
  1. calling functions
    my_function()

  2. Python Built-in Functions (不用 import 就可以使用)
    Built-in Functions — Python 3.9.1 documentation

click on [Reeborg’s keyboard], you can see all of the functions that we can use to command our robot

for & while 使用時機:

for迴圈以 “固定數量的變數”,作為控制條件的重點
while迴圈以 “某個條件”,作為控制條件的重點

2個讚

■ 6-58. The Hurdles Loop Challenge
沒什麼。對大家來說,應該蠻容易。
https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Hurdle%201&url=worlds%2Ftutorial_en%2Fhurdle1.json

PEP8 PYTHON 編碼規範手冊

2個讚

■ 6-59. Indentation in Python

用「資料夾」和「檔案」的方式說明縮排,很直觀又易懂,真是不錯。

8:10 設定 repl.it 的縮排空白鍵數量

PEP 8 – Style Guide for Python Code _ Python.org
PEP 8 -- Style Guide for Python Code | Python.org
PEP 8 -- Style Guide for Python Code | Python.org

in Python 3,
you can’t mix a code file that uses tabs and spaces for indentation in the same file.

3個讚

■ 6-60. While Loops
https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Hurdle%202&url=worlds%2Ftutorial_en%2Fhurdle2.json

while something_is_true:
	print("I'm good.")

while at_goal != true:
	jump()

while not at_goal():
	jump()

9:22
■ 何時使用 for?何時使用 while? ■ (請參考前面玉米的說明)

避免無窮迴圈的防呆

舉例來說:

loop_count = limit_number(例如:9)
while ((at_goal != true) and (loop_count > 0)):
	jump()
	loop_count -= 1
4個讚

徜徉在無窮迴圈的大海裡

1個讚

■ 6-61. Hurdles Challenge using While Loops
https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Hurdle%203&url=worlds%2Ftutorial_en%2Fhurdle3.json

front_is_clear()
wall_in_front()
at_goal()

while at_goal() != true:
	if front_is_clear():
		move()
	if wall_in_front():
		only_jump() # no move in the first step

■ 6-62. Jumping over Hurdles with Variable Heights

change jump function

def jump():
	turn_left()
	while not right_is_clear():
		move()
	turn_right()
	move()
	turn_right()
	while front_is_clear():
		move()
	turn_left()

■ 6-63. Final Project: Escaping the Maze
https://reeborg.ca/reeborg.html?lang=en&mode=python&menu=worlds%2Fmenus%2Freeborg_intro_en.json&name=Maze&url=worlds%2Ftutorial_en%2Fmaze1.json

掃地機器人簡單版

優先考慮右、或優先考慮左。固定就好,沒有哪個比較好,因為迷宮是亂數產生的。
因為之前課程都是向右前進,所以就以「優先考慮右」的方式撰寫。

wall 的定義,和地板的地義,假設是相同的。就是不能前進的(界線)。

可以右轉就右轉並前進、不然可以前進就前進、都不行就左轉;左轉後可前進就進前、不能前進就左轉再前進

1個讚
def next_step(): 
	if right_is_clear():
		turn_right()
		move()
	elif front_is_clear():
		move()
	else:
		turn_left()
		if front_is_clear():
			move()
		else:
			turn_left()
			if front_is_clear(): # 前方應該沒東西,不然四面都是牆,但還是防呆一下,也許出題的人程式沒寫好
				move() 
		
while not at_goal():
	next_step()

差不多,我截圖給你們看

螢幕快照 2021-02-07 下午8.59.42

要再判斷前面跟右邊是不是有路
讓他保持相右跟向前優先

1個讚

我也ok,只是我怕會影響倒室友