###A. 编程实现一个命令窗程序,使得:
#####输入“A”则在屏上回显“Your input is A”
输入“我”则在屏上回显“Your input is 我”
#####等等。
#####输入ByeBye则退出程序.
代码如下:
# -*- coding: UTF-8 -*-
a=1
while a ==1: # 该条件永远为true,循环将无限执行下去
s = raw_input("Please input:")
if s=="ByeBye":
print "ByeBye!"
exit()
print "You input is: ", s
结果如下:
###B. 编程实现一个命令窗程序,使得:
#####输入“A”则在屏上回显A字符的ASCII码。
输入“4”则在屏上回显4字符的ASCII码。
输入“我”则在屏上回显“我”字的汉字内码。
#####等等。
输入ByeBye则退出程序.
代码如下:
# -*- coding: UTF-8 -*-
a=1
while a ==1: # 该条件永远为true,循环将无限执行下去
s = raw_input("Please input:").decode("gbk")
if s=="ByeBye":
print "ByeBye!"
exit()
print s,"的ASCII码为:", ord(s)
结果如下:
###C. 编程实现一个命令窗程序,使得:
输入“你”则在屏上回显“you”。
输入“书”则在屏上回显“book”。
输入“中”则在屏上回显“middle”。
输入“中国”则在屏上回显“China”。
…要能输入至少100个词。如输入没有记录的词则如下:
输入“东东”则在屏上回显“查不到该词”。
输入ByeBye则退出程序.
(提示: 单词字典应做一个文本文件读入,其中每行为:<中文字词><对应英文> )
######如:字典文件 dic.txt内容是
######<我>< I >
#####<你>< you >
######<中国> < China >
代码如下:
# -*- coding: UTF-8 -*-
with open("dic.txt","r") as f: # 打开dic.txt文件
d=f.readlines();
# 查找函数
def findword(content):
chinese_content=""
english_content=""
flag=0
for dic_list in d:
chinese_content=dic_list.split("<")[1].split(">")[0] # 把“<”和">"作为分隔符对字符串进行切片
english_content=dic_list.split(">")[1].split("<")[1]
if content==chinese_content:
flag=1
return english_content
if flag==0:
english_content="查不到该词!"
return english_content
while(True):
s =raw_input("Please input:")
if s=="ByeBye":
print "退出程序!"
exit()
print(findword(s))
字典文件 dic.txt
结果如下: