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
  • 相关阅读:
    C语言学习笔记:14_内部函数和外部函数
    HDU 1247 Hat’s Words (字典树 &amp;&amp; map)
    混合云技术难题
    psql
    postgresql基本语句
    postgresql C/C++ API 接口
    REST API初识及设计
    postGreSQL数据库部署及简单使用
    IO-APIC
    OpenstackHigh-level-service
  • 原文地址:https://www.cnblogs.com/jingsheng99/p/8679500.html
Copyright © 2011-2022 走看看