5.2 某个CS教授给出了五分测验,等级为5-A, 4-B, 3-C, 2-D, 1-E, 0-F编写一个程序,接受测验分数作为输入,并打印出相应的等级。
# -*- coding: utf-8 -*-
#gradeconvert.py
def gradeconvert():
gradeLevel = "FEDCBA"
#获取分数
grade = float(input("Enter the grade: "))
print("The converted grade level is: ", gradeLevel[grade])
gradeconvert()
5.3. 某个CS教授给出了100分的考试,等级为90-100: A、80-89: B、70-79: C、60-69: D、<60:F。编写一个程序,接受测验分数作为输入,并打印出相应的等级。
# -*- coding: utf-8 -*-
#gradeconvert2.py
def gradeconvert2():
gradeLevel = "FDCBA"
#获取分数
grade = float(input("Enter the grade: "))
gradeLev = int(grade/10 - 5)
if gradeLev < 0:
print("The converted grade level is: F")
elif gradeLev == len(gradeLevel):
print("The converted grade level is: A")
else:
print("The converted grade level is: ", gradeLevel[gradeLev])
gradeconvert2()
5.4. 编写一个程序,允许用户键入一个短语,然后输出该短语的首字母缩略词(大写)。
# -*- coding: utf-8 -*-
#capInitials.py
def capInitials():
words = input("Please enter a phrase: ").split() #使用split将输入字符串转换为列表
init = "" #声明变量,赋值为空
for i in range(len(words)):
init = init + words[i][0]
init = init.upper() #字符串方法s.upper(),字符串s所有字母大写
print(init)
capInitials()
5.5. 编写程序,计算输入单个名字的数值。a为1,b为2,c为3......
# -*- coding: utf-8 -*-
#nameValue.py
def nameValue():
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nameStr = input("Please enter a your name: ") #使用split将输入字符串转换为列表
nameStr = nameStr.upper()
nValue = 0
for letter in nameStr:
nValue = nValue + alphabet.find(letter) + 1
print("Your name vlaue is: ", nValue)
nameValue()
5.6. 扩展前一个问题的解决方案,允许计算完整的名字。
# -*- coding: utf-8 -*-
#nameValue1.py
def nameValue1():
alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" #空格数值为0
nameStr = input("Please enter a your name: ") #使用split将输入字符串转换为列表
nameStr = nameStr.upper()
nValue = 0
for letter in nameStr:
nValue = nValue + alphabet.find(letter)
print("Your name vlaue is: ", nValue)
nameValue1()
5.7. 编写一个可以编码和解码凯撒密码的程序,键值为2。
# -*- coding: utf-8 -*-
#caesarCipher0.py
def caesarCipher0():
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
message = input("Enter the message that needs to be translated: ")
message = message.upper()
mode = input("Encode or decode: ")
transMsg = ""
key = 2
if mode == "encode" :
for letter in message:
if letter == " ":
transMsg = transMsg + " "
else:
loc = alphabet.find(letter) + key
if loc > 25: #循环移位
loc = loc - 26
transMsg = transMsg + alphabet[loc]
print("The encoded message is: ", transMsg)
elif mode == "decode":
for letter in message:
if letter == " ":
transMsg = transMsg + " "
else:
loc = alphabet.find(letter) - key
if loc < 0: #循环移位
loc = loc + 26
transMsg = transMsg + alphabet[loc]
print("The decoded message is: ", transMsg)
caesarCipher0()
5.8. 上一个练习有一个问题,它不处理超出字母表末端的情况。真正的凯撒密码以循环方式移动,其中z之后的下一个字母是a。修改上一个问题的解决方案,让它循环。
# -*- coding: utf-8 -*-
#caesarCipher.py
def caesarCipher():
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
message = input("Enter the message that needs to be translated: ")
message = message.upper()
mode = input("Encode or decode: ")
transMsg = ""
if mode == "encode" :
key = int(input("The key value is: "))
for letter in message:
if letter == " ":
transMsg = transMsg + " "
else:
loc = alphabet.find(letter) + key
if loc > 25: #循环移位
loc = loc - 26
transMsg = transMsg + alphabet[loc]
print("The encoded message is: ", transMsg)
elif mode == "decode":
print("The decoded messages are: ")
for key in range(26):
transMsg = ""
for letter in message:
if letter == " ":
transMsg = transMsg + " "
else:
loc = alphabet.find(letter) - key
if loc < 0: #循环移位
loc = loc + 26
transMsg = transMsg + alphabet[loc]
print("Key value = ", key, transMsg)
caesarCipher()
5.9. 编写一个程序,计算用户输入句子中的单词数。
# -*- coding: utf-8 -*-
#wordCal().py
def wordCal():
inputArr = input("Enter a sentence: ").split()
print("This sentence cotains ", len(inputArr), "words")
wordCal()
5.10. 编写一个程序,计算用户输入句子中的平均单词长度。
# -*- coding: utf-8 -*-
#wordAve.py
def wordAve():
inputStr = input("Enter a sentence: ")
inputArr = inputStr.split() #将输入字符串以空格为分界,分隔为字符串列表
numLetter = len(inputStr) - inputStr.count(" ")
numWord = len(inputArr)
print("The average lenth of the words in this sentence is: {0:0.2f}".format(numLetter/numWord))
wordAve()
5.11. 编写第1章中的chaos.py程序的改进版本,允许用户输入两个初始值和迭代次数,然后打印一个格式很好的表格,显示这些值随时间变化的情况。
# File:chaos.py
# -*- coding: utf-8 -*-
# 1.5、修改chaos程序,让打印值的数量由用户决定。
# File:chaos.py
# -*- coding: utf-8 -*-
# 5.11、修改chaos程序,让打印值的数量由用户决定。
def main():
print("This program illustrates a chaotic function.")
randNum = input("Enter two numbers between 0 and 1, separated by comma: ").split(",")
randNum = [float(x) for x in randNum ] #列表中字符串元素转为浮点型数字
n = eval(input("Enter the times of cycle: ")) #获取循环次数n
print("index\t{0}\t\t{1}".format(randNum[0], randNum[1]))
for i in range(n):
for j in range(2):
randNum[j] = 3.9 * randNum[j] * (1 - randNum[j])
print("{0}\t{1:0.8f}\t{2:0.8f}".format(i, randNum[0], randNum[1]))
main()
5.12. 编写第2章中的futval.py程序的改进版本。程序将提示用户投资金额、年化利率和投资年数。然后程序将输出一个格式正确的表,以年为单位跟踪投资的价值。
# File:2.6.py
# futval
# -*- coding:utf-8 -*-
def main():
print("This program calculates the future value of a n-year investment.")
principal = eval(input("Enter the initial principal: "))
apr = eval(input("Enter the annual interest rate: "))
year = eval(input("Enter the year of investment: ")) #输入投资年数
print("The value in each year is:\nYear\tValue")
print("--------------------------------")
for i in range(year): #循环次数与年数一致
principal = principal * (1 + apr)
print("{0}\t{1:.2f}".format(i + 1, principal)) #输出的年数与输入一致
main()