# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # 函数
#
# - 函数可以用来定义可重复代码，组织和简化
# - 一般来说一个函数在实际开发中为一个小功能
# - 一个类为一个大功能
# - 同样函数的长度不要超过一屏

# Python中的所有函数实际上都是有返回值(return None),
#
# 如果你没有设置return,那么Python将不显示None.
#
# 如果你设置return,那么将返回出return这个值.

def HJN():
    print('Hello')
    return 1000


b=HJN()
print(b)

HJN


def panduan(number):
    if number % 2 == 0:
        print('O')
    else:
        print('J')


panduan(number=1)

panduan(2)

# ## 定义一个函数
#
# def function_name(list of parameters):
#     
#     do something
# ![](../Photo/69.png)
# - 以前使用的random 或者range 或者print.. 其实都是函数或者类

# 函数的参数如果有默认值的情况,当你调用该函数的时候:
# 可以不给予参数值,那么就会走该参数的默认值
# 否则的话,就走你给予的参数值.

import random


def hahah():
    n = random.randint(0,5)
    while 1:
        N = eval(input('>>'))
        if n == N:
            print('smart')
            break
        elif n < N:
            print('太小了')
        elif n > N:
            print('太大了')



# ## 调用一个函数
# - functionName()
# - "()" 就代表调用

def H():
    print('hahaha')


def B():
    H()


B()


def A(f):
    f()


A(B)


# ![](../Photo/70.png)

# ## 带返回值和不带返回值的函数
# - return 返回的内容
# - return 返回多个值
# - 一般情况下，在多个函数协同完成一个功能的时候，那么将会有返回值

# ![](../Photo/71.png)
#
# - 当然也可以自定义返回None

# ## EP：
# ![](../Photo/72.png)

def main():
    print(min(min(5,6),(51,6)))
def min(n1,n2):
    a = n1
    if n2 < a:
        a = n2


main()


# ## 类型和关键字参数
# - 普通参数
# - 多个参数
# - 默认值参数
# - 不定长参数

# ## 普通参数

# ## 多个参数

# ## 默认值参数

# ## 强制命名

def U(str_):
    xiaoxie = 0
    for i in str_:
        ASCII = ord(i)
        if 97<=ASCII<=122:
            xiaoxie +=1
        elif xxxx:
            daxie += 1
        elif xxxx:
            shuzi += 1
    return xiaoxie,daxie,shuzi


U('HJi12')


# ## 不定长参数
# - \*args
# > - 不定长，来多少装多少，不装也是可以的
#   - 返回的数据类型是元组
#   - args 名字是可以修改的，只是我们约定俗成的是args
# - \**kwargs 
# > - 返回的字典
#   - 输入的一定要是表达式（键值对）
# - name,\*args,name2,\**kwargs  使用参数名

def TT(a,b)


def TT(*args,**kwargs):
    print(kwargs)
    print(args)
TT(1,2,3,4,6,a=100,b=1000)

{'key':'value'}

TT(1,2,4,5,7,8,9,)


def B(name1,nam3):
    pass


B(name1=100,2)


# +
def sum_(*args,A='sum'):
    
    res = 0
    count = 0
    for i in args:
        res +=i
        count += 1
    if A == "sum":
        return res
    elif A == "mean":
        mean = res / count
        return res,mean
    else:
        print(A,'还未开放')
    
    
# -

sum_(-1,0,1,4,A='var')

'aHbK134'.__iter__

b = 'asdkjfh'
for i in b :
    print(i)

2,5
2 + 22 + 222 + 2222 + 22222

# ## 变量的作用域
# - 局部变量 local
# - 全局变量 global
# - globals 函数返回一个全局变量的字典，包括所有导入的变量
# - locals() 函数会以字典类型返回当前位置的全部局部变量。

a = 1000
b = 10
def Y():
    global a,b
    a += 100
    print(a)
Y()


def YY(a1):
    a1 += 100
    print(a1)
YY(a)
print(a)


# ## 注意：
# - global ：在进行赋值操作的时候需要声明
# - 官方解释：This is because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope.
# - ![](../Photo/73.png)

# # Homework
# - 1
# ![](../Photo/74.png)

def getPentagonalNumber(n):
    count = 0
    for n in range(n):
        f = n * (3 * n - 1) / 2
        if  f != 0:
            print(int(f),end="   ")
            count += 1
        if count % 10 == 0 :
            print()
getPentagonalNumber(101)

# - 2 
# ![](../Photo/75.png)

n = input('Enter a number:')
def sumDigits(n):
    for i in range(len(n)):
        if int(n) > 0:
            bw = int(n) // 100 
            sw = int(n) // 10 % 10
            gw = int(n) % 10
            s = bw + sw + gw
    print('%d (%d+%d+%d)'%(s,bw,sw,gw))
sumDigits(n)

# - 3
# ![](../Photo/76.png)

# +
num1,num2,num3 = map(float,input('Enter three numbers:').split(' '))
a = [num1,num2,num3]
def displaySortedNumbers(num1,num2,num3):
    for i in range(len(a)):
        for j in range(0,len(a)-1):
            if a[j] > a[j+1]:
                t = a[j]
                a[j] = a[j+1]
                a[j+1] = t
        print('The sorted numbers:%d'%a[i])
        
displaySortedNumbers(num1,num2,num3)        
# -

# - 4
# ![](../Photo/77.png)

investmentAmount = float(input('The amount invested:'))
annuallyInterestRate = float(input('Annual interest rate:'))
def futureInvestmentValue(investmentAmount,annuallyInterestRate,years):
    import math
    print('Years     Future Value')
    for i in range(1,years+1):
        futureInvestmentValue = investmentAmount * ((1 + annuallyInterestRate / 100 / 12) ** (i * 12))
        print('%d         %.2f'%(i,futureInvestmentValue))
futureInvestmentValue(investmentAmount,annuallyInterestRate,30)


# - 5
# ![](../Photo/78.png)

ch1,ch2,numberPerLine = map(int,input('Enter the numbers:').split(' '))
def printChars(num1,num2,num3):
    count = 0
    for i in range(ch1,ch2):
        count += 1
        print(chr(i),end = " ")
        if count % numberPerLine == 0:
            print()   
printChars(ch1,ch2,numberPerLine)


# - 6
# ![](../Photo/79.png)

def numberOfDaysInAYear(year):
    for i in range(year,2021):
        if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0:
            print('%d年有%d天'%(i,366))
        else:
            print('%d年有%d天'%(i,365))
numberOfDaysInAYear(2010)

# - 7
# ![](../Photo/80.png)

x1,y1,x2,y2 = map(int,input('Enter the number:').split(' '))
def distance(x1,y1,x2,y2):
    import math
    dis = math.sqrt((x1 - x2) ** 2+(y1 - y2) ** 2)
    print(dis)
distance(x1,y1,x2,y2)


# - 8
# ![](../Photo/81.png)

def ML():
    print('p   2^p-1')
    for p in range(2,32):
        s = 2 ** p - 1
        print('%d    %d'%(p,s))
ML()    

# - 9
# ![](../Photo/82.png)
# ![](../Photo/83.png)

# Mine
import time
def datetime():
    Time_ = time.localtime(time.time())
    print('Current date and time is August %d, %d %d:%d:%d'%(Time_[2],Time_[0],Time_[3],Time_[4],Time_[5]))
datetime()

# Mine
import time
def datetime():
    Time_ = time.asctime(time.localtime(time.time()))
    print('Current date and time is August',Time_)
datetime()



import time

# +
import time

localtime = time.asctime(time.localtime(time.time()))
print("本地时间为 :", localtime)

# -

2019 - 1970



# - 10
# ![](../Photo/84.png)

# +
def three():
    print('You rolled %d + %d = %d'%(one,two,sum_))
    print('point is %d'%sum_)
    for i in range(1,3):
        one1 = random.randrange(1,7)
        two1 = random.randrange(1,7)
        sum_1 = one1 + two1
        if sum_ == sum_1:
            print('You rolled %d + %d = %d'%(one1,two1,sum_1))
            print('You win')
        elif sum_1 == 7:
            print('You rolled %d + %d = %d'%(one1,two1,sum_1))
            print('You lose')
def game():
    import random
    one = random.randrange(1,7)
    two = random.randrange(1,7)
    sum_ = one + two
    if sum_ == 2 or sum_ == 3 or sum_ == 12:
        print('You rolled %d + %d = %d'%(one,two,sum_))
        print('You lose')
    elif sum_ == 7 or sum_ == 11:
        print('You rolled %d + %d = %d'%(one,two,sum_))
        print('You win')
    elif sum_ == 4 or sum_ == 5 or sum_ == 6 or sum_ == 8 or sum_ == 9 or sum_ == 10:
        three()

game()
# -

# - 11 
# ### 去网上寻找如何用Python代码发送邮件

# +
# coding:utf-8
# smtplib模块负责连接服务器和发送邮件
# MIMEText：定义邮件的文字数据
# MIMEImage：定义邮件的图片数据
# MIMEMultipart：负责将文字图片音频组装在一起添加附件
import smtplib  # 加载smtplib模块
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.application import MIMEApplication
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart 
sender = '<EMAIL>'  # 发件人邮箱账号
receive = '<EMAIL>'  # 收件人邮箱账号
passwd = '<PASSWORD>'
mailserver = 'smtp.126.com'
port = '25'
sub = 'Python3 test'

try:
        msg = MIMEMultipart('related')
        msg['From'] = formataddr(["sender", sender])  # 发件人邮箱昵称、发件人邮箱账号
        msg['To'] = formataddr(["receiver", receive])  # 收件人邮箱昵称、收件人邮箱账号
        msg['Subject'] = sub
       #文本信息
       #txt = MIMEText('this is a test mail', 'plain', 'utf-8')
       #msg.attach(txt)
       #附件信息
#         attach = MIMEApplication(open("C:/Users/lenovo/Desktop/Classes_.txt").read())
#         attach.add_header('Content-Disposition', 'attachment', filename='Classes_.txt')
#         msg.attach(attach)
 
#        #正文显示图片
#         body = """
#            <b>this is a test mail:</b> 
#             <br><img src="cid:image"><br>
#            """
#         text = MIMEText(body, 'html', 'utf-8')
#         f = open('C:/Users/lenovo/Desktop/Classes_.txt', 'rb')
#         pic = MIMEImage(f.read())
#         f.close()
#         pic.add_header('Content-ID', '<image>')
#         msg.attach(text)
#         msg.attach(pic)
        line1 = "这是第一行"
        #邮件正文第一行内容
        line2 = "这是第二行"
        #邮件正文第二行内容
        line3 = "这是第三行"
        #邮件正文第三方内容

        msg = MIMEMultipart('related')
        msgtext = MIMEText("<font color=black>zabbix成绩表:<br>刘雅丽 99：%s<br>席斯娜 65：%s<br>赵琪琦 100：%s<br>详细内容见附件。</font>" %(line1, line2 ,line3), "html", "utf-8")
        msg.attach(msgtext)
 
        server = smtplib.SMTP(mailserver, port)  # 发件人邮箱中的SMTP服务器，端口是25
        server.login(sender, passwd)  # 发件人邮箱账号、邮箱密码
        server.sendmail(sender, receive, msg.as_string())  # 发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()
        print('success')
except Exception as e:
         print(e)

StarcoderdataJupyterScriptsDedupFiltered