CS61A Lab 1: Variables & Functions, Control


快速回顧

1. 命令行運行python

  • python3 foo.py,運行文件
  • python3 -i foo.py,運行python腳本,並打開一個交互式會話
  • python3 -m doctest foo.py,運行doctest

2. 命令行運行ok

  • python3 ok -q <specified function>, 運行指定函數
  • python3 ok -v, 模式不顯示已通過的樣例,-v可以全部顯示
  • print("DEBUG:", x), 調試打印功能

主題

1. 除法

  • True Division: / (decimal division),常規除法
  • Floor Division: // (integer division),整除
  • Modulo: % (remainder),取模

2. 函數及其調用

To evaluate a function call:
  1. Evaluate the operator, and then the operands (from left to right).
  2. Apply the operator to the operands (the values of the operands).
return and print介紹

3. Control

  • 布爾運算符 and,or,not
  • 短路特性 and,or
  • if 語句
  • while 循環

4. 錯誤信息

image

必答題 required questions

what would python display

Q1: WWPD: Control

python ok -q control -u --local

  • how_big(),注意print(' ')return ' '的引號區別。
  • while positive,while循環內部修改變量positive,但是positive不為0(負數也是True),無限循環。
Q2: WWPD: Veritasiness

python ok -q short-circuit -u --local

  • 理解短路特性較容易
Q3: Debugging Quiz!

python ok -q debugging-quiz -u --local
image

Coding Practice

Q4: Falling Factorial

python ok -q falling --local
caution:print or return

Q5: Sum Digits

python ok -q sum_digits --local
Hint:floor division and modulo
caution:需要返回值

Extra Practice 額外練習

Q6: WWPD: What If?

python ok -q if-statements -u --local
Hint: print (unlike return) does not cause the function to exit!
caution:print(' ')return ' '的引號區別

Q7: Double Eights

python3 ok -q if-statements -u --local
相鄰的兩個8:
return '88' in str(n)

lab01完成

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM