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

posted @ 2021-05-25 23:02  ikventure  阅读(1691)  评论(0编辑  收藏  举报