西南科技大学Python实验指导书部分代码

等腰三角

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/9 23:08
def sanjiao():
    for i in range(10):print("{}".format("*"*(2*i-1)).center(19," "))
if __name__=="__main__":
    sanjiao()

好好学习

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/9 23:11
if __name__=="__main__":
    print("365天好好学习:{}".format(1.01**365))
    print("365天不好好学习:{}".format(0.99**365))
    study=1
    for i in range(1,366):
        if i%7==0 or i%7==6:
           dtudy=study*0.99
        else:
            study=study*1.01
    print("学习:{}".format(study))

凯撒密码

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/9 23:21
def encryption(passwd):
    print("加密后的密码为:",end="")
    for i in passwd:print("{}".format(chr((ord(i)-ord("A")+3)%26+ord("A"))),end="")
if __name__=="__main__":
    passwd=input("请输入您的密码:")
    encryption(passwd)

猜数游戏

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/15 23:33
import random as  rand

flag  = rand.randint(1,100)
cout = 0
while True:
    cin = input("请输入一个1-100之间的整数:")
    try:
        eval(cin)
        if int(eval(cin)) != eval(cin):
            print("请输入整数:")
            continue
        cin = eval(cin)
        if cin == flag:
            cout = cout+1
            print("恭喜你猜对了!预测了{}次!".format(cout))
            st = input("还要继续玩吗!\n输入1继续,输入0结束:")
            if eval(st) == 1:
                cout = 0
                flag = rand.randint(1,100)
                continue
            else:
                exit()
        if cin < flag:
            cout = cout+1
            print("遗憾,太小了!")
        if cin > flag:
            cout = cout+1
            print("遗憾,太大了!")
    except NameError:
        print("输入格式错误,程序结束!")
        exit()


狐狸

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/11 19:52
import random as ran
flag = ran.randint(0,4)
tiao=[-1,1]
print(flag)
while True:
    try:
        inp = eval(input("please input a number 0-4:"))
    except:
        print("The type of input is error! input again!")
        continue
    if inp < 0 or inp > 4:
        print("Number range error!")
        continue
    if inp == flag:
        print("Get It!,Game over!")
        break
    else:
        flag+=tiao[ran.randint(0,1)]
        flag%=5
        print(flag)

三国

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/11 19:13
import jieba as jie
op = open("sanguo.txt","r",encoding='utf-8')
txt = op.read()
words = jie.lcut(txt)
nwords=["这个","没有","蜀汉","自己","这样","他们","东吴","如今","就是","然而","已经","虽然"]
counts={
     }
for i in words:
    if len(i)>1 and i not in nwords:
        counts[i] = counts.get(i,0)+1
counts = list(counts.items())
counts.sort(key=lambda x:x[1],reverse=True)
for i in range(5):
    print("{0:<10}{1:<5}".format(counts[i][0],counts[i][1]))
print(type(txt))

选素数

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/16 14:05
import math


def sushu(a):
    flag = True
    if a > 2:
        for i in range(2, int(math.sqrt(a)) + 2):
            if a % i == 0:
                flag = False
                break
    elif a == 2:
        return flag
    else:
        flag = False
    return flag


if __name__ == "__main__":
    li = []
    while True:
        a = input("请输入一个正整数,输入0则结束输入:")
        if a.isnumeric():
            if eval(a) == 0:
                break
            if sushu(eval(a)):
                li.append(eval(a))
            continue
        else:
            print("输入非法,请重新输入!")
    li=set(li)
    li=list(li)
    print("您输入的正整数中,属于素数的有:{}".format(li))

设置密码

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/16 14:51
def juge_mima(mima):
    shuzi = False
    daxie = False
    xiaoxie = False
    if len(mima) >= 8:
        for i in mima:
            if i >= "0" and i <= "9":
                shuzi = True
            elif i >= "a" and i <= "z":
                xiaoxie = True
            elif i >= "A" and i <= "Z":
                daxie = True
            else:
                return False
        if shuzi and xiaoxie and daxie:
            return True
    return False


def is_xiangtong(mima1, mima2):
    return mima1 == mima2


if __name__ == "__main__":
    print("密码规则:密码长度至少为8位,并且必须含有且只能含有字母大小写和数字该三种字符。")
    while True:
        mima1 = input("请输入您要设置的新密码:")
        if (juge_mima(mima1)):
            mima2 = input("请输入您要设置的密码确认:")
            if is_xiangtong(mima1, mima2):
                print("密码设置成功!")
                break
            else:
                print("设置密码与确认密码不匹配,请重新设置密码!")
                continue
        else:
            print("您输入的密码不符合标准,请重新设置密码!")

字符统计

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/16 14:36
if __name__ == "__main__":
    st = input("请输入一串字符,输入完毕按回车键:")
    counts = {
     }
    for i in st:
        counts[i] = counts.get(i, 0) + 1
    counts = list(counts.items())
    counts.sort(key=lambda x: x[1], reverse=True)
    print("出现次数最多的前三个字符如下:")
    for i in range(3):
        print("字符:{0:<10} 出现次数:{1:<5}".format(counts[i][0], counts[i][1]))
    zl = 0
    for i in counts:
        zl = zl + 1
    print("一共输入了{}种字符!".format(zl))

26个字母

# -*- coding:utf-8 -*-
# Author : ZhangPeng
# Data : 2019/10/18 20:04
def numlist():
    return [x for x in range(1, 26 + 1)]


def charlist():
    return [chr(x) for x in range(ord("A"), ord("Z") + 1)]


if __name__ == "__main__":
    print(dict(zip(numlist(), charlist())))

你可能感兴趣的:(Python学习,Python,实验)