<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
    5-3.

    Standard Type Operators. Take test score input from the user and output letter grades

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0503.py 126 2010-04-20 02:18:07Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10
    11'''
    12Print the score result.
    13'''
    14def getResultString(score):
    15    if score <or score > 100return "ERROR SCORE"
    16    if 90 <= score <= 100return "A"
    17    if 80 <= score <= 89return "B"
    18    if 70 <= score <= 79return "C"
    19    if 60 <= score <= 69return "D"
    20    return "F"
    21
    22if __name__ == '__main__':
    23    '''
    24    Take test score input from the user and output letter grades. 
    25    '''
    26    while True:
    27        s = raw_input("Enter an Integer ('exit' or 'quit' for over). \n>>")
    28        if s == 'exit' or s == 'quit' : break
    29
    30        try:
    31            v=int(s)
    32        except ValueError,e:
    33            print "Error! Not a number. ",
    34        else:
    35            print v,"=>",getResultString(v)
    36
    5-4.

    Modulus. Determine whether a given year is a leap year, using the following formula: a leap year is one that is divisible by four, but not by one hundred, unless it is also divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1967 and 1900 are not. The next leap year falling on a century is 2400.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0504.py 127 2010-04-20 02:19:33Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import sys
    11import types
    12
    13'''
    14Determine whether a given year is a leap year.
    15'''
    16def isleapyear(year):
    17    if type(year) is not types.IntType: return False    
    18    if year > 9999 or year <= 0: return False
    19    if (year%400 == 0) or ((year%4==0) and (year%100 !=0)):
    20        return True
    21    return False
    22
    23if __name__ == '__main__':
    24    '''
    25    Print leap year.
    26    '''
    27    while True:
    28        s = raw_input("Input a 'year' ('exit' or 'quit' for over). \n>>")
    29        if s == 'exit' or s == 'quit' :sys.exit(0)
    30
    31        try:
    32            year=int(s)
    33        except ValueError,e:
    34            print "Error! Not a number. ",
    35        else:
    36            print year,"is leap year?",isleapyear(year)
    37
    5-5.

    Modulus. Calculate the number of basic American coins given a value less than 1 dollar. A penny is worth 1 cent, a nickel is worth 5 cents, a dime is worth 10 cents, and a quarter is worth 25 cents. It takes 100 cents to make 1 dollar. So given an amount less than 1 dollar (if using floats, convert to integers for this exercise), calculate the number of each type of coin necessary to achieve the amount, maximizing the number of larger denomination coins. For example, given $0.76, or 76 cents, the correct output would be "3 quarters and 1 penny." Output such as "76 pennies" and "2 quarters, 2 dimes, 1 nickel, and 1 penny" are not acceptable.


     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0505.py 128 2010-04-20 02:22:45Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10
    11'''
    12get min number of cents
    13'''
    14def getMinNumberOfCents(m):
    15    if m < 0 or m >= 1return (-1,-1,-1,-1
    16    money = round(m*100)
    17    yy25 = divmod(money,25)
    18    yy10 = divmod(yy25[1],10)
    19    yy5 = divmod(yy10[1],5)
    20    return (int(yy25[0]),int(yy10[0]),int(yy5[0]),int(yy5[1]))
    21
    22if __name__ == '__main__':
    23    '''
    24    Calculate the number of basic American coins given a value less than 1 dollar.
    25    '''
    26    while True:
    27        s = raw_input("Input a 'money' between 0 and 1 ('exit' or 'quit' for over). \n>>")
    28        if s == 'exit' or s == 'quit' :break
    29
    30        try:
    31            money=float(s)
    32        except ValueError,e:
    33            print "Error! Not a number. ",
    34        else:
    35            print "25 cents: %d\n10 cents: %d\n 5 cents: %d\n 1 cents: %d" % getMinNumberOfCents(money)
    36
    5-6.

    Arithmetic. Create a calculator application. Write code that will take two numbers and an operator in the format: N1 OP N2, where N1 and N2 are floating point or integer values, and OP is one of the following: +, -, *, /, %, **, representing addition, subtraction, multiplication, division, modulus/remainder, and exponentiation, respectively, and displays the result of carrying out that operation on the input operands. Hint: You may use the string split() method, but you cannot use the exal() built-in function.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0506.py 129 2010-04-20 02:25:57Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import sys
    11
    12'''
    13a two number calculator
    14'''
    15def calculate(s):
    16    for c in ('+','-','**','/','%','*'):
    17        rs = s.split(c)
    18        if rs and len(rs) == 2:
    19            one=float(rs[0].strip())
    20            two=float(rs[1].strip())
    21            if c == '+':return one+two
    22            if c == '-':return one-two
    23            if c == '**'return one**two
    24            if c == '/'
    25                if two==0: raise ValueERROR,"ZERO DIVISION"
    26                return one/two
    27            if c == '%':return one%two
    28            if c == '*':return one*two
    29            raise ValueError,"ERROR EXPRESSION"
    30    raise ValueError,"ERROR EXPRESSION"
    31            
    32
    33if __name__ == '__main__':
    34    '''
    35    Write code that will take two numbers and an operator in the format: N1 OP N2.
    36    '''
    37    while True:
    38        s = raw_input("Input a string expression ('exit' or 'quit' for over). \n>>")
    39        if s == 'exit' or s == 'quit' :break
    40
    41        try:
    42            v = calculate(s)
    43        except ValueError,e:
    44            print "Error! Not a expression. ",
    45        else:
    46            print "%s = %s" % (s,v)
    47
    5-17.

    *Random Numbers. Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 231-1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0517.py 130 2010-04-20 02:29:51Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import sys
    11import random
    12
    13'''
    14a random demo
    15'''
    16def randomAndSort(m,max):
    17    ret = []
    18    for i in range(m):
    19        ret.append(random.randint(0,max))
    20    ret.sort()
    21    return ret
    22
    23if __name__ == '__main__':
    24    '''
    25    Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 2e31-1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.
    26    '''
    27    print randomAndSort(100,sys.maxint)
    28


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

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 国产美女精品久久久久久久免费 | 亚洲av日韩综合一区久热| 亚洲成a人片77777kkkk| 国产高清免费的视频| 国产男女爽爽爽爽爽免费视频| 一级一片免费视频播放| 亚洲AV无码专区亚洲AV桃| 亚洲三级视频在线观看| 久久亚洲精品中文字幕| 亚洲狠狠婷婷综合久久久久| 免费在线观看中文字幕| 日韩在线视频免费看| 国产免费av片在线看| 美女视频黄是免费的网址| 在线免费观看亚洲| 久久免费动漫品精老司机| 中文字幕无码毛片免费看| 国产成人无码精品久久久免费| 免费看一级一级人妻片| 特级毛片全部免费播放a一级| 亚洲精品永久在线观看| 在线a亚洲老鸭窝天堂av高清| 91亚洲精品麻豆| 亚洲中文字幕久在线| 亚洲冬月枫中文字幕在线看| 亚洲欧洲校园自拍都市| 777亚洲精品乱码久久久久久 | 亚洲成a人片毛片在线| 亚洲美女自拍视频| 久久久久亚洲av无码专区导航| 亚洲第一精品福利| 亚洲福利在线视频| 久久亚洲精品中文字幕| 亚洲综合日韩中文字幕v在线| 亚洲一区二区中文| 亚洲国语在线视频手机在线| 亚洲国产精品一区二区久| 久久亚洲精品专区蓝色区| 国产午夜亚洲精品| 亚洲avav天堂av在线网毛片| 国产精品日本亚洲777|