Python核心编程第5章部分练习题

#-*-coding: UTF-8 -*-

def myMul(a, b):    # 5-2
    return a * b

def judge(score):   # 5-3
    'Judge your score, give level prompt'
    if score > 100 or score < 0:
        print '错误输入'
        return None
    elif score >= 90:
        return 'A'
    elif score >= 80:
        return 'B'
    elif score >= 70:
        return 'C'
    elif score >= 60:
        return 'D'
    else:
        return 'F'

def isLeapYear(year):   # 5-4
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    return False

def changeCoin(money):  # 5-5
    values = [25, 10, 5, 1]
    remain = money
    nums = []
    for i in xrange(len(values)):
        nums.append(remain // values[i])
        remain -= nums[i] * values[i]
    return nums

def myEval(exp = ''):   # 5-6
    ops = ['+', '-', '*', '/', '%', '**']
    if (len(exp) < 3):
        print 'too short'
        return None
    for i in xrange(len(ops)):
        nums = exp.split(ops[i], 1)
        if len(nums) < 2:
            continue
        elif ops[i] == '+':
            return float(nums[0]) + float(nums[1])
        elif ops[i] == '-':
            return float(nums[0]) - float(nums[1])
        elif ops[i] == '*':
            return float(nums[0]) * float(nums[1])
        elif ops[i] == '/':
            return float(nums[0]) / float(nums[1])
        elif ops[i] == '%':
            return float(nums[0]) + float(nums[1])
        elif ops[i] == '**':
            return float(nums[0]) ** float(nums[1])
        else:
            print 'no valid operators'
            return None

# 5-9
'''
(a) python中数字以0开头表示8进制
'''
       
# 5-10
'''
from __future__ import division
'''
原文地址:https://www.cnblogs.com/robert-cai/p/3452942.html