第三周——小结 Day6 7.17

今天周末,做了4个程序

1.分别使用for循环、while循环,打印出九九乘法表(循环知识)

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

2.使用if语句,实现石头剪刀布的游戏效果(条件判断知识)

(1)普通版:

import random

print("*******欢迎来到猜拳游戏!*******")
print("***********游戏规则***********")
print("****剪刀-0****石头-1****布-2****")
print("**********开始PK吧!**********")

player = int(input("请输入你的数字:"))
computer = random.randint(0,2)
print("对方出的是:%d"%computer)
if (player == 0 and computer == 2) or (player == 1 and computer == 0) or (player == 2 and computer == 1):
    print("获胜!哈哈,你太厉害了!")
elif player == computer :
    print("平局,要不再来一局?")
else:
    print("输了,不要走,决战到天亮!")

(2)plus版(纠错+重复游戏)

import random

print("*******欢迎来到猜拳游戏!*******")
print("***********游戏规则***********")
print("****剪刀-0****石头-1****布-2****")
print("**********开始PK吧!**********")

while True:
    player =int(input("请输入你的数字:"))
    computer = random.randint(0, 2)
    if player >= 0 and player <= 2:
        print("对方出的是:%d" % computer)
        if (player == 0 and computer == 2) or (player == 1 and computer == 0) or (player == 2 and computer == 1):
            print("获胜!哈哈,你太厉害了!")
        elif player == computer :
            print("平局,要不再来一局?")
        else:
            print("输了,不要走,决战到天亮!")
    else:
        print("输入无效数字,请重新输入")
        continue
    p = input("输入Y继续游戏,其他键结束游戏:")
    if p == "Y":
        print("********************")
    else:
        print("*******游戏结束*******")
        break

(3)pro版:(用差做条件,纠错+重复游戏)

import random

print("*******欢迎来到猜拳游戏!*******")
print("***********游戏规则***********")
print("****剪刀-0****石头-1****布-2****")
print("**********开始PK吧!**********")

while True:
    player = int(input("请输入你的数字:"))
    computer = random.randint(0, 2)
    c = player - computer
    if c == -2 or c == 1:
        print("对方出的是:%d" % computer)
        print("获胜!哈哈,你太厉害了!")
    elif c == 0 :
        print("对方出的是:%d" % computer)
        print("平局,要不再来一局?")
    elif c == -1 or c ==2:
        print("对方出的是:%d" % computer)
        print("输了,不要走,决战到天亮!")
    else:
        print("输入无效数字,请重新输入")
        continue
    p = input("输入Y继续游戏,其他键结束游戏:")
    if p == "Y":
        print("********************")
    else:
        print("*******游戏结束*******")
        break

(4)创新版:(用列表做条件)

import random

while True:
    player = input("剪刀为0,石头为1,布为2,请输入0,1,2 出拳:")
    a = ["0","1","2"]
    win = [["1","0"],["2","1"],["0","2"]]
    computer = random.choice(a)
    if player in a :
        print("对方出的是:%s" % computer)
        if player == computer:
            print("平局,要不再来一局?")
        elif [player,computer] in win :
            print("获胜!哈哈哈,你太厉害了!")
        else:
            print("输了,不要走,决战到天亮!")
    else:
        print("输入无效数字,请重新输入")
        continue
    p = input("输入Y继续游戏,其他键结束游戏:")
    if p == "Y":
        print("********************")
    else:
        print("*******游戏结束*******")
        break

 

3.(列表知识)

现有商品列表如下:

(1)products=[["iphone",6888],["MacPro",14800],["小米6",2499],["coffee",31],["Book",60],["Nike",699]],需打印以下格式:

------------   商品列表   ------------

0               iphone              6888

1               MacPro            14800

2               小米6                2499

3               coffee               31

4               Book                60

5               Nike                 699

(2)根据上面的products列表写一个循环,不断询问用户想要买什么,用户选择一个商品编号,就把对应的商品添加到购物车,最终用户输入q退出时,打印购买的商品列表。

products=[["iphone",6888],["MacPro",14800],["小米6",2499],["coffee",31],["Book",60],["Nike",699]]

print("------商品列表------")

for i , item in enumerate(products):
    print(str(i)+"\t"+item[0]+"\t"+str(item[1]))

shopping_car = [0 for i in range(6)]
count = 0
sum = 0

while True:
    id = input("请输入您要购买的商品编号:")
    if id.isdigit():
        id = int(id)
        if id < 0 or id >= len(products):
            print("商品编码超出范围,请重新输入")
            continue
        else:
            shopping_car[id] += 1
            count += 1
    elif id.isalpha() and id == 'q':
        if count == 0:
            print("您没有购买任何商品,欢迎下次光临!")
        else:
            print("------您已购买------")
            i = 0
            for num in shopping_car:
                if num != 0:
                    print(products[i][0]+"\t"+str(products[i][1])+"\t"+str(num)+"个"+"\t"+str(products[i][1]*num)+"元")
                    sum += products[i][1]*num
                i += 1
            print("共计:"+ str(sum) + "元")
        exit()
    else:
        print("输入格式有误,请输入数字")

4.(文件知识)

(1)应用文件操作的相关知识,通过python新建一个文件poem.txt,选择一首古诗写入文件中

(2)另外写一个将内容复制到copy.txt中,并在控制台输出“复制完毕”(可以用函数,也可以不用)

极简版:

f = open ("poem.txt","w")
f.write('''夏日绝句
                  李清照
         生当作人杰,
         死亦为鬼雄。
         至今思项羽,
         不肯过江东。
''')
f.close()

with open("poem.txt",'r') as f1:
    with open("copy.txt",'w') as f2:
        f2.write(f1.read())
        print("复制完毕")

函数版:

def write_file(file_name,content):
    with open(file_name,'w',encoding='utf-8') as file:
        file.write(content)

def copy_file(source_file,target_file):
    with open(source_file,'r',encoding='utf-8') as sourcefile:
        with open(target_file,'w',encoding='utf-8') as targetfile:
            targetfile.write(sourcefile.read())

        print("复制完毕")

content = '''夏日绝句
                  李清照
            生当作人杰,
            死亦为鬼雄。
            至今思项羽,
            不肯过江东。
'''

write_file('poem.txt',content)
copy_file('poem.txt','copy.txt')

小结:这周一边学习python基本语法,一遍跟着做项目,目前可以做较复杂的程序,下周开始学习爬虫,顺便补点前端的知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值