<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 評論 :: 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毛片免费| 亚洲日本在线免费观看| 黄页网站在线看免费| 国产精品免费电影| 亚洲性日韩精品一区二区三区| 亚洲国产美女精品久久久久∴ | 国产免费女女脚奴视频网| 国产精品视频永久免费播放| 国产精品免费看久久久久| 亚洲色大成网站www永久一区| 久久亚洲精品中文字幕| 亚洲熟妇av午夜无码不卡| 一区二区在线视频免费观看| 无码午夜成人1000部免费视频| 丁香花免费完整高清观看| 亚洲国产激情一区二区三区| 亚洲AV日韩AV永久无码久久| 亚洲自国产拍揄拍| 无码人妻一区二区三区免费视频 | 国产成人无码免费看片软件| 久9久9精品免费观看| 国内外成人免费视频| 亚洲理论电影在线观看| 亚洲av无码片在线观看| 一级特黄特色的免费大片视频| 99视频在线看观免费| 国产精品冒白浆免费视频| 久久青青成人亚洲精品| 亚洲AV永久无码精品一福利| 亚洲免费观看视频| 免费高清小黄站在线观看| 亚洲精品高清国产一线久久| 亚洲人成小说网站色| 99久久成人国产精品免费| 免费看少妇作爱视频| 久久99国产亚洲精品观看| 亚洲一久久久久久久久| 免费视频一区二区| 国产一级理论免费版|