码迷,mamicode.com
首页 > 编程语言 > 详细

Python基础学习(一)

时间:2017-07-03 21:02:21      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:key   tutorial   default   min   信息   error:   als   with   ted   

#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基础学习(一)

标签:key   tutorial   default   min   信息   error:   als   with   ted   

原文地址:http://www.cnblogs.com/cathyj/p/7112847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!