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

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

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

    xylz,imxylz

    關(guān)注后端架構(gòu)、中間件、分布式和并發(fā)編程

       :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評(píng)論 :: 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 閱讀(16815) 評(píng)論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 亚洲∧v久久久无码精品| 国产成人亚洲综合网站不卡| 91久久成人免费| 亚洲宅男天堂a在线| 国产做床爱无遮挡免费视频| 成人国产精品免费视频| 亚洲最大视频网站| 免费在线观看毛片| 亚洲视频免费一区| 立即播放免费毛片一级| 亚洲AV永久纯肉无码精品动漫| 18禁无遮挡无码网站免费| 国产三级在线免费观看| 亚洲人成免费电影| 亚洲综合av永久无码精品一区二区 | 亚洲国产夜色在线观看| 亚洲第一区精品日韩在线播放| 最近2019中文字幕免费大全5| 精品亚洲福利一区二区| 亚洲五月六月丁香激情| 免费人成无码大片在线观看| 麻豆国产精品免费视频| a级黄色毛片免费播放视频| 亚洲成a人片在线观看天堂无码 | 久久99青青精品免费观看| 看一级毛片免费观看视频| 亚洲精品在线网站| 久久亚洲AV永久无码精品| 好男人看视频免费2019中文| 免费观看在线禁片| 无码精品人妻一区二区三区免费 | 亚洲一级片免费看| 亚洲精品精华液一区二区| 久久亚洲精品无码VA大香大香| 亚洲精品成人区在线观看| 卡一卡二卡三在线入口免费| 在线观看免费中文视频| 你懂的免费在线观看| 男女猛烈无遮掩视频免费软件| 中文字幕亚洲综合久久综合| 91久久亚洲国产成人精品性色|