zoukankan      html  css  js  c++  java
  • 【Python】练习题

    练习1:有两个磁盘文件A和B,各存放一行字母,要求把这两个文件中的信息合并(按字母顺序排列), 输出到一个新文件C中
    import os
    file1_path="e:\test3\2.txt"
    file2_path="e:\test3\3.txt"
    file3_path="e:\test3\4.txt"
    content=""
    with open(file1_path) as f1:
        content=f1.read().strip() #不能用f1.readlines(),因为返回一个list,list不能strip,字符串才能
           
    with open(file2_path) as f2:
        content+=f2.read().strip()
     
    result=list(content) #list才能排序,这里将string转换成list
    result.sort()
    #result=sorted(list(content))
     
    with open(file3_path,"w") as f3:
        f3.write("".join(result)) #写文件用的string,"".join将list转换成string
     
     
    #练习3:题目:输入一个奇数,然后判断最少几个 9 除以该数的结果为整数。
    while 1:
        try:
            num=int(raw_input("please input a number:"))
            if num % 2 ==1:
                break
            else:
                print u"这个数字不是奇数哦"
        except ValueError:
            print "ValueError"
            continue
        except:
            print "Unknow Error occurred"
            continue
     
    i=1
    while i<=20:
        if int("9"*i)%num==0: #%整除该数
            print u"%s 个9可以整除 %s"%(str(i).decode("utf-8"),str(num).decode("utf-8"))
            break
        else:
            i+=1
     
    while i>20:
        print u"啊哦~几个9也不能整除该数字~"
        break
  • 相关阅读:
    图片反转效果
    css实现三角效果
    漂亮的阴影效果
    css名词解释
    偷学来的资料
    Git、GitHub、GitLab三者之间的联系以及区别
    分模块、分工程管理
    Spring AOP面向切面编程
    为什么要用存储过程,什么时候要用存储过程,存储过程的优点
    Spring扫描组件的使用详解
  • 原文地址:https://www.cnblogs.com/jingsheng99/p/8679500.html
Copyright © 2011-2022 走看看