<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
    主站蜘蛛池模板: 国产日韩成人亚洲丁香婷婷| 99久久免费精品高清特色大片| 亚洲天堂2017无码中文| 亚洲黄色网址大全| 91亚洲国产成人久久精品网站| 亚洲AV午夜成人片| 亚洲精品~无码抽插| 亚洲精品成人网站在线观看| 亚洲无线码一区二区三区| 国产亚洲综合成人91精品 | 亚洲日韩国产一区二区三区在线| 亚洲图片激情小说| 亚洲伊人色一综合网| tom影院亚洲国产一区二区| 亚洲一区二区三区在线网站| 亚洲丰满熟女一区二区v| 亚洲乱码一二三四区乱码| 亚洲高清一区二区三区| 亚洲国产午夜精品理论片在线播放 | 成人a毛片视频免费看| 一级一级毛片免费播放| CAOPORM国产精品视频免费| 国产免费内射又粗又爽密桃视频 | 久久水蜜桃亚洲av无码精品麻豆| 久久亚洲精品无码| 亚洲精品自在线拍| 亚洲一区二区三区无码国产| 亚洲日韩av无码中文| 鲁啊鲁在线视频免费播放| 一个人看的在线免费视频| 精品国产一区二区三区免费| 无码av免费网站| 真人做人试看60分钟免费视频| 成人免费a级毛片无码网站入口| 女人被男人躁的女爽免费视频| 日本v片免费一区二区三区| 亚洲精品无码永久在线观看| 国产成人无码综合亚洲日韩| 亚洲黄色在线网站| 亚洲成a人片在线观看天堂无码| xvideos永久免费入口|