痘疤脸

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  

 

  继续学习 继续尝试 拿了几个小例子来熟悉一下 一些特殊的功能

1、使用 strip 来移除 字段两边的空格。

   name = " booM "
   a = name.strip()
   print(a)

2、使用endswith、starswith 来判断开头结尾。

name = "booM"
a = name.endswith("oM")
print(a)




name = "booM"
a = name.startswith("bo")
print(a)

3、使用replace 来替换指定的字段。

name = "booM"
a = name.replace("o","p")
print(a)

4、使用join将_ 添加到 字符串中。

a= "hfkasjhfkjashf"
v="_".join(a)
print(v)

5、用现有的知识做出 5+5这种 低级加法运算的尝试 使用 for 和 isdecimal 来进行判断 或者直接用 split 将字段分割。

shuru = input("qingshuru")
he=0
for ceshi in shuru:
    panduan=ceshiu.isdecimal()
    if panduan==True :
        he=he+int(ceshi)
print(he)


#或者用 split 的方法
he=0
shuru = input("qingshuru")
v1,v2=shuru.split("+")
he=vi+v2
print(he)

6、 对输入的字段进行类型判断 只支持数字和字母  使用 isdecimal 和isalpha 来判断。

shuru = input("qingshuru")
shuzi=0
zimu=0
for ziduan in shuru :
    panduan = ziduan.isdecimal()
    if panduan == True:
        shuzi = shuzi + 1
    panduan = ziduan.isalpha()
    if panduan == True:
        zimu=zimu+1
print("字母数量"+str(zimu))
print("数字数量"+str(shuzi))

7、输入信息 输出特定字段包含信息  使用format来进行替换,使用过程中知道了{}中的数值必须要以0开始。

test = 'woshi {0} wolaizhi {1}'
name=input("你的姓名")
didian = input("你的家乡")
txt = test.format(name,didian)

print(txt)

8、验证码登录 模块的尝试  网上摘取random验证码 模块,使用while循环,设置了一个开关,或者直接True无线循环下去然后用break来跳出循环。

def check_code():
    import random
    checkcode=""
    for i in range(4):
        current = random.randrange(0,4)
        if current != i:
            temp = chr(random.randint(65,90))
        else:
            temp = random.randint(0,9)
        checkcode += str(temp)
    return checkcode


#  正式代码
code=check_code()

kaiguan =2
while kaiguan<3:
    code=check_code()
    print("验证码为:" + str(code.lower()))
    zhi = input("请输入验证码")
    if zhi.lower() == code :
        print("恭喜你输入正确")
        kaiguan+=1
    else:
        print("错误,重新测试")

# 或者使用 这种方式
while True:
    code = check_code()
    print("验证码为:" + str(code.lower()))
    zhi = input("请输入验证码")
    if zhi.lower() == code.lower() :
        print("恭喜你输入正确")
        break

 9、制作表格,循环输入 用户名 密码  邮箱  要求输入值只读取前20字符,然后用表格输出,输入q 退出。  简单尝试,使用center居中更好看 并且限制字段数目 然后明白了 \t 和\n制表符的使用。

kaiguan = 0
txt=""
while True :
    yonghuming= input("请输入你的姓名")
    yonghuming=yonghuming[0:20]
    mima= input("请输入你的密码")
    mima=mima[0:20]
    yuxiang = input("请输入你的邮箱")
    yuxiang=yuxiang[0:20]
    if yonghuming == "q" or mima == "q" or yuxiang=="q":
        break
    else:
        txt=txt+yonghuming.center(20," ") +"\t"+mima.center(20," ")+"\t" + 

yuxiang.center(20," ")+"\n"
print(txt)

  

  这些小题目一共使用了大概1个小时完成,难度一般 对于循环这个 方法现在掌握的比较透彻。

 

posted on 2018-08-16 14:11  痘疤脸  阅读(149)  评论(0编辑  收藏  举报