<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xylz,imxylz

    關注后端架構、中間件、分布式和并發編程

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks

    2-5.

    Loops and Numbers. Create some loops using both while and for.

    1. Write a loop that counts from 0 to 10 using a while loop. (Make sure your solution really does count from 0 to 10, not 0 to 9 or 1 to 10.)

    2. Do the same loop as in part (a), but use a for loop and the range() built-in function.

     1 #!/usr/bin/env python
     2 #-*- coding:utf-8 -*-
     3 #$Id: p0205.py 121 2010-04-19 15:20:40Z xylz $
     4 
     5 '''
     6 This is a 'python' study plan for xylz.
     7 Copyright (C)2010 xylz (www.imxlz.info)
     8 '''
     9 
    10 if __name__ == '__main__':
    11     '''
    12     print 0 to 10 with while loop or for loop
    13     '''
    14     i=0
    15     print "using while: ".rjust(15),
    16     while(i<=10):
    17         print i,
    18         i+=1
    19     print
    20 
    21     i=0
    22     print "using for: ".rjust(15),
    23     for i in range(11):
    24         print i,
    25     print 
    26 
    27     del i
    28 

    2-6.

    Conditionals. Detect whether a number is positive, negative, or zero. Try using fixed values at first, then update your program to accept numeric input from the user.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0206.py 120 2010-04-19 15:20:05Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxlz.info)
     8'''
     9
    10if __name__ == '__main__':
    11    '''
    12    Detect whether a number is positive, negative, or zero.
    13    '''
    14
    15    s=raw_input("input a number ('quit' or 'exit' for over): ")
    16    while s != 'exit' and s != 'quit':
    17        try:
    18            i=int(s)
    19            if i > 0:print "you enter(positive): %d > 0" % i
    20            elif i <0 :print "you enter(negative): %d < 0" %i
    21            else:print "you enter(zero): %d = 0" %i
    22        except ValueError,e:
    23            print "Error! Not a Integer"
    24        s=raw_input("input a number ('quit' or 'exit' for over): ")
    25
    26

    2-7.

    Loops and Strings. Take a user input string and display string, one character at a time. As in your above solution, perform this task with a while loop first, then with a for loop.


     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0207.py 122 2010-04-19 15:31:21Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxlz.info)
     8'''
     9
    10import sys
    11from datetime import time
    12import time
    13
    14if __name__ == '__main__':
    15    '''
    16    Get a string from terminal and print it one by one.
    17    '''
    18    while True:
    19        s = raw_input("input a string ('quit' or 'exit' for over). \n>>")
    20        if s == 'exit' or s == 'quit':break
    21        if s:
    22            i=0
    23            while i < len(s):
    24                c=s[i]
    25                sys.stdout.write(c)
    26                time.sleep(0.1)
    27                i+=1
    28            print
    29            for c in s:
    30                sys.stdout.write(c)
    31                time.sleep(0.1)
    32            print
    33
    34

    2-8.

    Loops and Operators. Create a fixed list or tuple of five numbers and output their sum. Then update your program so that this set of numbers comes from user input. As with the problems above, implement your solution twice, once using while and again with for.


     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0208.py 123 2010-04-19 15:34:50Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxlz.info)
     8'''
     9
    10import sys
    11
    12if __name__ == '__main__':
    13    '''
    14    Get file number from terminal and print the sum and avg.
    15    '''
    16    listdata = [0,0,0,0,0]
    17    index,v=0,0
    18    while True:
    19        s = raw_input("Input a Number ('quit' or 'exit' for over). \n>>")
    20        if s == 'exit' or s == 'quit':sys.exit(0)
    21        try:
    22            v=float(s)
    23            listdata[index] = v
    24            index += 1
    25            if index == len(listdata):break
    26        except ValueError,e:
    27            print e
    28            print "Error! Not a number. ",
    29
    30    tupdata = tuple(listdata)
    31
    32    (index,tupsum,listsum) = (0,0.0,0.0)
    33    while index<len(listdata):
    34        tupsum+=tupdata[index]
    35        index+=1
    36
    37    for i in listdata:
    38        listsum+=i
    39
    40    print "tupsum: %.2f, avg: %.2f" % (tupsum,tupsum/len(listdata))
    41    print "listsum: %.2f, avg: %.2f" % (listsum,listsum/len(listdata))
    42
    2-15.

    Elementary Sorting.

    1. Have the user enter three numeric values and store them in three different variables. Without using lists or sorting algorithms, manually sort these three numbers from smallest to largest.

    2. How would you change your solution in part (a) to sort from largest to smallest?


     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0215.py 124 2010-04-19 15:39:29Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import sys
    11
    12if __name__ == '__main__':
    13    '''
    14    Get some Integers and print it those are sorted.
    15    '''
    16    data=[]
    17    while True:
    18        s = raw_input("Input a Integer ('exit' for over, 'quit' for sort). \n>>")
    19        if s == 'exit':sys.exit(0)
    20        elif s == 'quit':break
    21
    22        try:
    23            v=int(s)
    24            data.append(v)
    25        except ValueError,e:
    26            print "Error! Not a number. ",
    27    for i in range(0,len(data),1):
    28        for j in range(i+1,len(data),1):
    29            if data[i] > data[j]: data[i],data[j] = data[j],data[i]
    30    print "-->: ",
    31    for i in data:
    32        print i,
    33    print
    34
    35    print "<--: ",
    36    for m in data[::-1]:
    37        print m,
    38    print
    39
    40


    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2010-04-19 22:46 imxylz 閱讀(16886) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 成人亚洲国产精品久久| 亚洲va中文字幕无码| 亚洲AV无码成人精品区在线观看 | 久久久精品免费视频| 国产精品久久免费| 亚洲精品无码激情AV| 日韩精品免费一线在线观看| 久操免费在线观看| 亚洲AV成人片色在线观看| 精品四虎免费观看国产高清午夜| 亚洲日韩精品无码一区二区三区 | 野花香高清在线观看视频播放免费| 亚洲乱码无码永久不卡在线| 在线观看免费视频一区| 亚洲AV无码一区二区三区DV| 九九精品国产亚洲AV日韩| 日本人护士免费xxxx视频| 激情婷婷成人亚洲综合| 亚洲男人的天堂在线va拉文| 国产成人免费ā片在线观看老同学| 亚洲AV日韩AV天堂一区二区三区 | 久久精品免费一区二区喷潮 | 亚洲精品专区在线观看| 中文字幕在线免费看线人| 久久久久久久亚洲Av无码| 日韩毛片免费无码无毒视频观看| 国产成A人亚洲精V品无码| 高清免费久久午夜精品| 国产精品酒店视频免费看| 黄网站色成年片大免费高清 | 亚洲熟女综合色一区二区三区| 日本一道高清不卡免费| 最近的2019免费中文字幕| 久久亚洲最大成人网4438| 亚洲精品国产高清嫩草影院| 1000部啪啪毛片免费看| 老司机午夜在线视频免费观| 亚洲视频在线观看免费| 永久中文字幕免费视频网站| 在线看片免费人成视频福利| 亚洲成a∨人片在无码2023|