Python编程实例解析与代码实现

41、根据用户输入的评分,报告员工的表现是不可接受、可接受还是优异。

Report whether an employee’s performance is unacceptable, acceptable or meritorious

based on the rating entered by the user.

RAISE_FACTOR = 2400.00
UNACCEPTABLE = 0
ACCEPTABLE = 0.4
MERITORIOUS = 0.6

# Read the rating from the user
rating = float(input("Enter the rating: "))

# Classify the performance
if rating == UNACCEPTABLE:
    performance = "Unacceptable"
elif rating == ACCEPTABLE:
    performance = "Acceptable"
elif rating >= MERITORIOUS:
    performance = "Meritorious"
else:
    performance = ""

# Report the result
if performance == "":
    print("That wasn’t a valid rating.")
else:
    print("Based on that rating, your performance is %s." % performance)
    print("You will receive a raise of $%.2f." % (rating * RAISE_FACTOR))

The parentheses around rating * RAISE_FACTOR on the final line are necessary because the % and * operators have the same precedence. Including the parentheses forces Python to perform the mathematical calculation before formatting its result.

42、判断某一年是否为闰年

以下是判断某一年是否为闰年的代码逻辑:首先,根据规则,任何能被400整除的年份是闰年;在剩下的年份中,能被100整除的年份不是闰年;在剩下的年份中,能被4整除的年份是闰年;其他年份都不是闰年。代码实现如下:

# Determine if it is a leap year
if year % 400 == 0:
    isLeapYear = True
elif year % 100 == 0:
    isLeapYear = False
elif year % 4 == 0:
    isLeapYear = True
else:
    isLeapYear = False

# Display the result
if isLeapYear:
    print(year, "is a leap year.")
else:
    print(year, "is not a leap year.")

其中 year 为待判断的年份。

43、判断一个车牌是否有效。有效的车牌格式为:1) 3个字母后接3个数字;2) 4个数字后接3个字母

# Read the plate from the user
plate = input("Enter the license plate: ")

# Check the status of the plate and display it. It is necessary to check each of the 6 characters
# for an older style plate, or each of the 7 characters for a newer style plate.
if len(plate) == 6 and \
plate[0] >= "A" and plate[0] <= "Z" and \
plate[1] >= "A" and plate[1] <= "Z" and \
plate[2] >= "A" and plate[2] <= "Z" and \
plate[3] >= "0" and plate[3] <= "9" and \
plate[4] >= "0" and plate[4] <= "9" and \
plate[5] >= "0" and plate[5] <= "9": 
    print("The plate is a valid older style plate.")
elif len(plate) == 7 and \
plate[0] >= "0" and plate[0] <= "9" and \
plate[1] >= "0" and plate[1] <= "9" and \
plate[2] >= "0" and plate[2] <= "9" and \
plate[3] >= "0" and pla
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值