CS61A HOG
CS61A project hog
以后记得每天git push ^^(git使用:参考)
知识1:请阐述global与nonlocal在作用域方面的差异?
使用global关键字可以在模块级别修改全局变量。
当使用nonlocal关键字定义嵌套函数时,默认该函数可修改其外层作用域中的同名局部变量(若外层无相同名称的局部变量则会引发错误)。
知识2:在Python中,请问如何通过一行代码交换两个变量?
答案是利用元组 unpacking功能:a, b = b, a。
问题1:为什么在连续调用同一函数时会触发内部嵌套函数?答案确定:内部嵌套函数通过return被返回值所指调用。
问题2
注意:
1.当一个函数返回另一个函数(称为f)后,在f内部定义过的局部变量仍然可以被f所访问。
2.重点 在编写闭包时应避免引用那些可能会随着时间发生变化或处于不断更新状态的量。
问题3:不理解函数何时调用肿么办???
重点:遇到f()这样的就是调用一次
问题4:Python中的递归会导致死循环吗?
不用担心啦!返回值是一个函数,并非直接进行额外的函数调用。特别提醒:必须明确什么时候进行函数调用。
def make_test_dice(*outcomes):
index = len(outcomes) - 1
print(“make_test_dice”,index,id(index))
def dice():
nonlocal index
index = (index + 1) % len(outcomes)
print(“dice”,index,id(index))
return outcomes[index]
return dice
test_dice = make_test_dice(4,2,1)
print(test_dice())
print(test_dice())
print(test_dice())
print(type(test_dice))
print(type(make_test_dice))
print(type(make_test_dice(4,2,1)))
print(test_dice is make_test_dice(4,2,1))
以下哪一个正确
Choose the number of the correct choice:
0) six_sided
注意返回值是不是函数,一定要随时都清楚变量是什么类型
遍历 num_rolls 的值:
遇到 TypeError 错误:尝试将整数对象作为可迭代对象使用失败。
原因分析:
num_rolls 为整数类型,
无法直接将整数赋值给迭代器,
必须将其转换为 range 对象后才能执行循环操作。
list l 的最大值的坐标:l.index(max(l))
Problems 12 放弃了( ̄▽ ̄)"
