成长6 - 定义函数学习,每天进步一点点!

今天的学习了定义函数命令,def~走你!

1、来个最简单的,定义一个函数func,接下来调用出这个函数,就是这个德行啦~

>>> def func():
    print('you are the best!')
    print('Yes!Keep Going!')

    
>>> func()
you are the best!
Yes!Keep Going!

2、进阶版,来个组合,逻辑就是定义order 函数,将命令合成,也就是说我定义了一个以name为变量的order函数,我想要得到当我个name赋值时,将值套在给的框架里。

从我输的编码里你看到啥?对喽~错了呗!字符没加引号,结果嘎嘎的都是错呀!不服气的我,再次一次,吼吼,搞定!就问你6不6~(请原谅一个自我感觉良好的Python小白.....)

>>> def order(name):
    print(name+', you are the best!')
>>> order(Michelle)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    order(Michelle)
NameError: name 'Michelle' is not defined
>>> order('Michelle')
Michelle, you are the best!

3、在进阶,多字段组合,看着很简单就出来了,谁知道背后一把辛酸泪呀,这个简单的命令姐试了5遍才搞定,真的是笨笨的向前爬啊~~~T__T~~~~

擦把鼻涕,继续讲下面的逻辑,三个字段组合,n是变量,然后就是运行咯,试了9,成功!还想更完美,换成了9th,结果又悲剧了,再次忽略了引号........,再来!自己当初选择要学的东西,跪着也要学下去!~~~~T__T~~~~

>>> def option(n):
    print('this is the' ,n,'day with Python!');

    
>>> option(9)
this is the 9 day with Python!
>>> option(9th)
SyntaxError: invalid syntax
>>> option('9th')
this is the 9th day with Python!

4、最后跟着课程又编了个略显白痴的程序

def discount(price,rate):
    final=price*rate
    return final

old=float(input('输入价格:'))
rates=float(input('输入折扣:' ))
new=discount(old,rates)
print('折后价:',new)

运行时是这样婶儿的,问你输入价格,我输100;问你输入折扣,我输0.8,回个车,就出来折后价啦,啦啦啦啦~果然很白痴.....哈哈哈~~~~

输入价格:100
输入折扣:0.8
折后价: 80.0

就到这里吧,虽然笨笨的我不奢望成为优秀的程序员,但至少学习这个东西永远不嫌多不是?!每天都给自己打气!!!

posted @ 2018-10-10 16:33  michellema  阅读(146)  评论(0编辑  收藏  举报