PYTHON Fantasy Adventures 16 Function ~ Food

PYTHON Fantasy Adventures 16 Function Food

Preface

Hello everyone! Xiaopengpeng is here again! Welcome everyone to the fantasy world of PYTHON! Today we are going to learn a concept that everyone has never seen before! function!

Every day, it only takes up ten minutes for everyone to eat at noon. If you have anything to do, take a look at it and master a language easily! Let us work hard together.
Insert picture description here

function

What is a function? What does the function look like? Is the function difficult? Don't worry, everyone! The function is very simple!
y= 2x+1
this is a function! ! !

x is the independent variable, 2x+1 is the execution process, and y is the result

Keep it simple!
In the world of PYTHON, functions become even simpler!
Your input is X, output is Y, and PYTHON is loaded with execution functions!
Taller, some functions of PYTHON itself are built-in functions, also called built-in functions!

for example

#直接运行代码即可    
a = len('流川枫我爱你')
print(a)
b = len(['佩奇','喷漆','配齐'])
print(b)
c = len(['PTTHON','C++','SystemVerilog'])
print(c)

answer

6
3
3

Everyone sees that there is no len(), this is the function! Everything in the brackets is a parameter! ! !

Here comes the problem

Can Xiaopengpeng want to create a function by himself? Think about it, everyone? Can it? The answer is yes, you need to create it yourself! ! !

Just use our example just now!
y=2x+1

def tommi_wei(x):
    y = 2*x + 1
    return y

It's that simple, have you learned it?
What if it changes now? X^2?

def math_tommi(x):
    y = x ** 2 + 'x'
    return y

It's that simple!

Now we continue to understand it step by step!

What is y if x=10 now?

def math_tommi(x):
    y =  x ** 2 + x
    return y

a = math_tommi(10)
print(a)

Answer:
110

Need to explain to everyone: the reason why you want to write this way! math_tommi(10), is the assignment, a is the result we finally print out!

Now what if I want to assign a value of 20 and 30?
What should be the answer?

def math_tommi(x):
    y = x ** 2 + x
    return y
a = math_tommi(20)
print(a)
print(math_tommi(30))

answer:

420
930

How about it, everyone should understand it now!

Actual practice

Now we need to print out the length of a sentence! For example: "young man, rat tail juice"

How to do? ? ?
Baidu? Or do you rely on yourself?

def tommi_len(words):
    counter = 0
    for i in words:
        counter = counter + 1
    return counter

Teacher_Ma = '年轻人,耗子尾汁'
print(tommi_len(Teacher_Ma))

Answer:
8

How did everyone learn it?

Continue studying

Not much to say, in the world of PYTHON, time is the most precious thing. So we went directly to the code and didn't talk about anything. As long as everyone sees it, the problem will be fully understood!
The next code is like this, we must pay attention to the details! Attention to detail! What Teacher Ma said, I was careless! I hope everyone will not be careless!

def Ma_laoshi():
    print('我大意了,没有闪,很快~')
    print('年轻人不讲武德,耗子尾汁')
    print('年轻人,这样好吗')

Ma_laoshi()

answer:

我大意了,没有闪,很快~
年轻人不讲武德,耗子尾汁
年轻人,这样好吗

It's almost noon. Xiao Pengpeng and his friends are also hungry! How to do? Order takeaway? It's too low!

def menu(cai, mian):
    print('一份炒菜:' + cai)
    print('一份主食:'   + mian)

menu('宫保鸡丁','三鲜面')

operation result:

一份开胃菜:宫保鸡丁
一份主食:三鲜面

In the world of PYTHON, satisfy all your fantasies about food!

Of course you may not feel enough to eat! no problem! Keep creating! ! !

Good food, come in the bowl! ! !

def menu(cai, mian, meishi='饺子'):
    print('一份炒菜:'  + cai)
    print('一份主食:'  + mian)
    print('一份美食:'  + meishi)

menu('宫保鸡丁','三鲜面')

operation result:

一份炒菜:宫保鸡丁
一份主食:三鲜面
一份美食:饺子

What about this time? Xiaopengpeng’s Pikachu said, it doesn’t like to eat dumplings, it likes to eat batteries, what should I do? Can only satisfy Pikachu! ! !

Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!


def menu(cai, mian, meishi='饺子'):
    print('一份炒菜:'  + cai)
    print('一份主食:'  + mian)
    print('一份美食:'  + meishi)

menu('宫保鸡丁','牛肉面')
print('**************************************')
menu('宫保鸡丁','牛肉面','build your dream电池')

operation result

一份炒菜:宫保鸡丁
一份主食:牛肉面
一份美食:饺子
**************************************
一份炒菜:宫保鸡丁
一份主食:牛肉面
一份美食:build your dream电池

The difficulty continues to escalate

What if I want to eat all the food in the world? Can PYTHON's fantasy adventure world satisfy my fantasy? The answer is yes!
All you have to do is yell "Ms. Tommi, help me" and all problems can be solved!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!

# 不定长参数要加星号
def tommi_menu(*all_kinds_of_foods):
    return all_kinds_of_foods

meishi = tommi_menu('牛排','羊肉汤','宫保鸡丁')
#括号里的这几个值都会传递给参数barbeque

print(meishi)
print(type(meishi))

operation result

('牛排', '羊肉汤', '宫保鸡丁')
<class 'tuple'>

Some questions about the print() function

print(*objects, sep = ' ', end = '\n', file = sys.stdout, flush = False)

You can see that the object is marked with an asterisk, because this is a manifestation of variable length parameters!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!


print('小红', '小王', '校长')
print('小红', '小王', '校长', sep = '+')
# sep控制多个值之间的分隔符,默认是空格
print('小红', '小王', '校长', sep = '+', end = '=?')

operation result:

小红 小王 校长
小红+小王+校长
小红+小王+校长=?

Next, the difficulty really comes! Everyone must study carefully! A lot of knowledge can only be comprehended by ourselves, instead of letting others teach you! To be honest, maybe others do not understand as much as you!
Not much to say, look at the code first, in the world of PYTHON, satisfy your fantasy together!
It's like this now! You can eat one cold dish for less than five yuan, and one cold dish and one battery for more than five yuan! ! !

import random 
xiaochi = ['牛肉片','土豆丝','夫妻肺片']
def wallet(money):
    if money < 5:
        a = random.choice(xiaochi)
        return a
    elif 5 <= money < 10:
        b = random.choice (xiaochi)
        return b, '电池'

print(wallet(7))
print(type(wallet(7)))

operation result:

('土豆丝', '电池')
<class 'tuple'>

Let's find a question together! Solved as a BUG

import random 
xiaochi = ['牛肉片','土豆丝','夫妻肺片']
def wallet(money):
    if money < 5:
        a = random.choice(xiaochi)
        return a
    elif 5 <= money < 10:
        b = random.choice(xiaochi)
        return b, '电池'
result = wallet(7)
print(wallet[0])
print(wallet[1])

Think about how to modify the above code?

import random 
xiaochi = ['牛肉片','土豆丝','夫妻肺片']
def wallet(money):
    if money < 5:
        a = random.choice(xiaochi)
        return a
    elif 5 <= money < 10:
        b = random.choice(xiaochi)
        return b, '电池'
result = wallet(7)
print(result[0])
print(result[1])

operation result:

土豆丝
电池

Welcome everyone to read my "The Weakness of Human Nature" to give you a different feeling!

Guess you like

Origin blog.csdn.net/weixin_46259642/article/details/112424971