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

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



