• Pyhton核心编程-Chap2习题-DIY


    在学Python,在看《Python核心编程》的pdf,做了Chap2的题目,答案为DIY

    # Filename: 2-11.py
    # Author: ChrisZZ
    mylist = [1, 2, 3, 4, 6]
    sum = 0
    for i in mylist:
        sum = sum + i
    average = sum * 1.0 / len(mylist)
    while True:
        print 'Here we have a list:', mylist
        option = raw_input('Whatyou gonna know(sum/average/exit)?')
        if option == 'sum':
            print 'the sum of the list is', sum
        elif option == 'average':
            print 'the average of the list is', average
        elif option == 'exit':
            print 'Bye'
            break
        else:
            print 'Wrong option.Repeat.'
    
    # Filename: 2-5.py
    # Author: ChrisZZ
    i = 0
    while i <= 10:
        print i
        i = i + 1
    
    for j in range(11):
        print j
    
    # Filename: 2-15.py
    # Author: ChrisZZ
    print 'Please input 3 number.'
    print 'I will sort them without using sort algo'
    a = float(raw_input('the first number'))
    b = float(raw_input('the second number'))
    c = float(raw_input('the third number'))
    res1 = (a - b) * (a - c)
    mylist = []
    if res1 < 0:  # a is the middle
        if b > c:
            mylist = [b, a, c]
        else:
            mylist = [c, a, b]
    else:
        if a > b and b > c:
            mylist = [a, b, c]
        elif a > c and c > b:
            mylist = [a, c, b]
        elif a < b and b < c:
            mylist = [c, b, a]
        elif a < c and c < b:
            mylist = [b, c, a]
    
    print mylist
    
    # Filename: 2-10.py
    # Author: ChrisZZ
    while True:
        num = float(raw_input('Enter a number in range(1,100):'))
        if num > 100 or num < 0:
            print 'not a good number. repeat.'
        else:
            print 'nice number.bye'
            break
    
    # Filename: 2-8.py
    # Author: ChrisZZ
    mylist = [1, 2, 3, 4, 5]
    sumW = 0
    i = 0
    while i < len(mylist):
        sumW = sumW + mylist[i]
        i = i + 1
    print sumW
    
    sumF = 0
    for i in mylist:
        sumF = sumF + i
    print sumF
    
    # Filename: 2-7.py
    # Author: ChrisZZ
    s = raw_input('Enter a string:')
    print 'while loop:'
    i = len(s)
    while i > 0:
        print s[-i]
        i = i - 1
    
    print 'for loop:'
    for ch in s:
        print ch
    
    # Filename: 2-6.py
    # Author: ChrisZZ
    num = float(raw_input('Enter a number:'))
    if num < .0:
        print 'Negative number'
    elif num > .0:
        print 'Positive number'
    else:
        print 'Zero'
    

      PS:这些题目都是分开写的py脚本,自己写了另一个脚本把他们重定向到了一个叫做result.txt的文件中,习题在~/workspace/python/xiti/路径,处理的脚本在~/workspace/python/,具体如下:

    # Filename: process.py
    # Author: ChrisZZ
    import os
    prefix = "/home/chris/workspace/python/xiti/"
    filenames = os.listdir(prefix)
    out = open('result.txt', 'w')
    for k, v in enumerate(filenames):
        f = open(prefix + v, 'r')
        out.write('# Filename: %s
    ' % v)
        out.write('# Author: ChrisZZ
    ')
        for eachLine in f:
            out.write(eachLine)
        f.close()
        out.write('
    ')
    out.close()
    
  • 相关阅读:
    Token 分析
    maven导入依赖下载jar包速度太慢
    springboot 自动装配
    @ComponentScan
    mysql8.0忘记密码或出现Access denied for user 'root'@'localhost' (using password: YES)
    SpringBoot静态资源处理
    @RestController
    PythonGUI:Tkinter学习笔记01
    Python2和Python3有什么区别?
    Python的Random模块
  • 原文地址:https://www.cnblogs.com/zjutzz/p/3253987.html
Copyright © 2020-2023  润新知