Python基础语法作业

Python 基础语法练习

  • Python基础语法作业
    • 猜拳
    • 九九乘法表
    • 打印商品
    • 购物车打印+结算
    • 函数
    • 写入古诗+复制古诗

Python基础语法作业

这是本人于2021年7月5号开始学习爬虫的作业汇总

猜拳

import  random #引入随即库

your_number = int(input("请输入:剪刀(0),石头(1),布(2): "))

print("你的输入为:%d" %your_number)

random_number = random.randint(0,2)

print("随机生成的数字为:%d" %random_number)

if your_number > 2 or your_number < 0 :
    print("只能输入0-2之间的整数!")
else :
    if your_number == random_number:
        print("平局")
    elif your_number == 0 and random_number == 2:
        print("恭喜,你赢了")
    elif your_number == 0 and random_number == 1:
        print("哈哈,你输了")
    elif your_number == 1 and random_number == 2:
        print("哈哈,你输了")
    elif your_number == 1 and random_number == 0:
        print("恭喜,你赢了")
    elif your_number == 2 and random_number == 1:
        print("恭喜,你赢了")
    elif your_number == 2 and random_number == 0:
        print("哈哈,你输了")

九九乘法表

#九九乘法表
i = 1
j = 1

for i in range(1,10,1):
    for j in range(1,10,1):
        while j <= i:
            print("%d*%d=%d"%(i,j,(i*j)),end=" ")
            j = j+1
        else:
            break
    print(" ")

打印商品

products = [["iphone",6888],["MacPro",14800],["小米6",2499],["coffee",31],["Book",60],["Nike",699]]
counter = 0
print("-"*5+"\t"+"商品列表"+"\t"+"-"*5)
for product in products:
    print("%d"%counter,end="\t")
    for i in range(2):
        print("%s"%products[counter][i],end="\t")
    print("\n")
    counter += 1

购物车打印+结算

products = [["iphone",6888],["MacPro",14800],["小米6",2499],["coffee",31],["Book",60],["Nike",699]]
shopping_goods = []
i = 0
count = 0
sum_money = 0
while True:
    goods = input("请输入你想购买的商品编号:")
    print("你的输入商品为:%s" % goods)
    if goods != "q" and -1 < int(goods) < 6:
        for i in range(2):
            shopping_goods.append(products[int(goods)][i])
        sum_money += products[int(goods)][1]
        print("%s已添加至购物车" % goods)
    elif goods == "q":
        print("购物结束")
        print("-" * 5 + "\t" + "加购商品列表" + "\t" + "-" * 5)
        for count in range(0,len(shopping_goods),2):
            print(shopping_goods[count],end="\t")
            print(shopping_goods[count+1], end="\t")
            print("\n")
        print("-" * 25)
        print("合计:%d元"%sum_money)
        break
    else:
        print("请输入正确的货物编号!")

函数

函数第1题:写一条打印一条横线的函数

#(1)
def printline():
    print("-"*30)

printline()
#(2)
def printline():
    a=("-"*30)
    return a

line  = printline()
print(line)

函数第2题:通过输入参数,打印自定义行数的横线

#(1)
def printline():
    print("-"*30)

def time(a):
    for i  in range(a):
        printline()

time(5)
#(2)
def printline():
    a=("-"*30)
    return a

def time(b):
    for i  in range(b):
        line = printline()
        print(line)

time(5)

函数第3题:求三个数字的和

#(1)
def sum3Num(a,b,c):
    d = a + b + c
    print(d)

sum3Num(5,6,7)
#(2)
def sum3Num(a,b,c):
    sum =  a + b + c
    return sum

result = sum3Num(5,6,7)

print(result)

函数第4题:求三个数字的平均数

def sum3Num(a,b,c):
    sum =  a + b + c
    return sum

def mean3Num(a,b,c):
    sum = sum3Num(a,b,c)
    mean = sum/3
    return mean

result = mean3Num(5,6,7)
print(result)

写入古诗+复制古诗

def creatFile():  # 创建古诗txt
    f = open("gushi.txt", "w", encoding="utf-8")
    f.write("""                 望庐山瀑布
                                  李白 
                            日照香炉生紫烟,遥看瀑布挂前川。
                            飞流直下三千尺,疑是银河落九天。""")
    f.close()
def copyFile():  # 读取文件
    f = open("gushi.txt","r",encoding="utf-8")
    contents = f.readlines()
    f.close()
    p = open("copy.txt", "w",encoding="utf-8")
    for content in contents:
        p.write(content)
try:
    creatFile()
    print("gushi文件已创建")
    copyFile()
    print("gushi文件已复制")

except Exception as result:
     print("发生异常")

你可能感兴趣的:(爬虫,Python学习,python,爬虫)