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:
- Evaluate the operator, and then the operands (from left to right).
- Apply the operator to the operands (the values of the operands).
return and print介绍
3. Control
- 布尔运算符 and,or,not
- 短路特性 and,or
- if 语句
- while 循环
4. 错误信息
必答题 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
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)