1. 程式人生 > >Python基礎學習(一)

Python基礎學習(一)

func exe function 學習 typeerror exit invalid min eas

#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 normal
ask_ok(‘‘, -5, ‘a‘)
#The last branch is wrong
ask_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



Python基礎學習(一)