#Default Argument Values & in keyword def ask_ok(prompt, retres=4, reminder=‘please try again!‘): while True: ok = input(prompt) if ok in (‘y‘, ‘yes‘, ‘ye‘): return True if ok in (‘n‘, ‘no‘, ‘nop‘, ‘nope‘): return False if retres < 0: print("err") return ValueError(‘invalid user response‘) print(reminder) #All branches are normalask_ok(‘‘, -5, ‘a‘)#The last branch is wrongask_ok(‘‘, ‘-5‘, ‘a‘) 函数的默认参数学习和in关键字学习。
ask_ok(‘‘, ‘-5‘, ‘a‘)由于参数类型不匹配,提示错误信息如下:
D:\Python3.6.1\python.exe F:/python_workspace/tutorial/TestFunction.py
aaaa
Traceback (most recent call last):
File "F:/python_workspace/tutorial/TestFunction.py", line 18, in <module>
ask_ok(‘‘, ‘-5‘, ‘a‘)
File "F:/python_workspace/tutorial/TestFunction.py", line 11, in ask_ok
if retres < 0:
TypeError: ‘<‘ not supported between instances of ‘str‘ and ‘int‘
Process finished with exit code 1
时间: 2024-11-03 21:14:43