python练习题

练习1.回文数

num = eval(input("输入三位整数"))
if (num//100 > 0 and num // 100<10):
    print("三位数")
    b = num // 100  #百位
    c = num % 10    #个位
    if b == c:
        print("是回文数")
    else:
        print("不是回文数")
else:
    print("不是三位数")

练习2.棋盘

import turtle
step = 20
turtle.showturtle()#显示
for i in range(11):
    turtle.penup()
    turtle.goto(0,step*i)
    turtle.pendown()
    turtle.forward(step*10)
turtle.right(270)
for i in range(11):
    turtle.penup()
    turtle.goto(step*i,0)
    turtle.pendown()
    turtle.forward(step*10)
turtle.dot(10,"red")
turtle.done()

练习3.三个数比大小

a,b,c = eval(input("a,b,c"))
if a>b:
    pass
    if a>c:
        print("max",a)
        if b>c:
            print(b,c)
        else:
            print(c,b)
else:
    pass
    if b >c :
        print("max",b)
        if c>a:
            print(c,a)
        else:
            print(a,c)
    else:
        print("max",c)
        if a > b:
            print(a, b)
        else:
            print(b, a)

练习5.啤酒2元1瓶,4个啤酒盖子可以免费换1瓶啤酒,2个空瓶子可以免费换1瓶啤酒现有10元,不允许再有别的规则,那么总共可以喝多少瓶啤酒?

m = 10
beers = m // 2
caps = 0    #啤酒
bottles = 0 #瓶盖
count = 0   #总和

while beers > 0:
    caps = caps + beers
    bottles = bottles + beers
    count = count + beers
    print('这次喝掉%d瓶酒,总计%d瓶酒'%(beers,count))
    beers = 0   #初始和酒瓶

    print('%d 瓶啤酒,%d 个瓶盖,%d 个瓶子'%(beers,caps,bottles))
    if  caps >=4:
        print('%d个盖子换%d瓶啤酒' % (caps - caps % 4, caps // 4))
        beers = beers+caps // 4
        caps = caps % 4
    elif bottles >= 2:
        print('%d个盖子换%d瓶啤酒' % (bottles - bottles % 4, bottles // 4))
        beers = beers + bottles//2
        bottles = bottles%2
        print('%d 瓶啤酒,%d 个瓶盖,%d 个瓶子' % (beers, caps, bottles))
    print("总共喝掉%d,剩下瓶啤酒%d个盖子,和%d空瓶"%(count, caps, bottles))

练习6.输入省份查询城市

sheng = {}  #省和市的集合
def tianjia(s,c):   #s代表省 c代表市
    if s not in sheng.keys():
        sheng[s] = c
    else:
        print(s+'已经存在')



print(sheng)
def show(): #显示以存储的省和市
    for p in sheng.keys():
        print(p,sheng[p])

def seeksheng(s):
    if s in sheng.keys():
        print(s,end=':')
        for i in sheng[s]:
            print(i,end=' ')

        print()
    else:
        print('没有这个省')
def seekct(c):
    for i in sheng.keys():
        if c in sheng[i]:
            print(c+"属于"+i+'省')
            return
        print('没有这个城市')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值