大作业|Python大作业一

Python大作业一
这学期拔尖计划又安排了一次Python课程的学习,因为之前学习过了所以就没有记录平时的编程作业,把大作业记录一下~~

print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 1") print("--------------------") def displayDictionary(): len_dic = len(dictionary) if len_dic == 0: print("*** The dictionary is empty ***") else: print("*** Known words ****") for i in range(len(dictionary)): a = dictionary[i] print(a[0].ljust(20),a[1].ljust(20))dictionary = [] displayDictionary() dictionary = [['the','这'],['big','大'],['house','房子']] displayDictionary()print("-------------------") print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 2") print("--------------------")flag=0 dictionary = [['the','这'],['big','大'],['house','房子']] def translateFromEnglish(word): lookup(word) return lookup(word) def lookup(word): for i in dictionary: if word == i[0]: flag=1 return i[1]w = translateFromEnglish('house') print('Translation of','house','is',w)w = translateFromEnglish('dog') print('translation of','dog','is',w)

print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 3") print("--------------------")def main(): while True: flag = 0 reply = input("Eng: > ") if reply == '?': displayDictionary() else: translateReply = [] words = list(reply.strip('.,?').split(' ')) for word in words: translation = lookup(word.lower()) translateReply.append(translation) if translation == None: print('None') flag = 1 break if flag == 0: str1 = ' '.join(translateReply) if words[0] in ['what','where','why','how','is']: m='?' else: m='.' print(str1.capitalize()+m) if flag ==1: breakdictionary = [['the','la'],['red','resso'],['is',"e'"],['table','tavola']]def translateFromEnglish(word): lookup(word) return lookup(word) def lookup(word): for i in dictionary: if word == i[0]: #flag = 1 return i[1]def displayDictionary(): len_dic = len(dictionary) if len_dic == 0: print("*** The dictionary is empty ***") else: print("*** Known words ****") for i in range(len(dictionary)): a = dictionary[i] print(a[0].ljust(10),a[1].ljust(10))main()

print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 4") print("--------------------")def main(): global dictionary while True: flag = 0 reply = input("Eng: > ") if reply == '?': displayDictionary() else: translateReply = [] words = list(reply.strip('.,?').split(' ')) for word in words: translation = lookup(word.lower()) if translation == None: print('How do i translate',word,':') tran1=input() dictionary = dictionary+[[word,tran1]] translateReply.append(tran1) #flag = 1 #break else: translateReply.append(translation)if flag == 0: str1 = ' '.join(translateReply) if words[0] in ['what','where','why','how','is']: m='?' else: m='.' print(str1.capitalize()+m) if flag ==1: breakdictionary = [['the','la'],['red','resso'],['is',"e'"],['table','tavola']] def translateFromEnglish(word): lookup(word) return lookup(word) def lookup(word): for i in dictionary: if word == i[0]: #flag = 1 return i[1]def displayDictionary(): len_dic = len(dictionary) if len_dic == 0: print("*** The dictionary is empty ***") else: print("*** Known words ****") for i in range(len(dictionary)): a = dictionary[i] print(a[0].ljust(10),a[1].ljust(10))main()

print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 5") print("--------------------")#从txt文件中获取字典dict = open('dictionary.txt','r+') dictionary = list(dict) #print(dictionary)#将读取到的内容排列成正常格式for i in range(len(dictionary)): dic1 = dictionary[i] flag = 0 for j in dic1: if j == '/': flag = 1 if flag == 0: raise Exception("程序运行错误") dic1 = dic1.strip('\n').split('/') #print(dic1) dictionary[i] = dic1#main()函数 def main(): global dictionary while True: flag = 0 reply = input("Eng: > ") if reply == '?': displayDictionary()#让程序可以正常结束 elif reply == 'break1': breakelse: translateReply = [] words = list(reply.strip('.,?').split(' ')) for word in words: translation = lookup(word.lower()) if translation == None: print('How do i translate',word,':') tran1=input() dictionary = dictionary+[[word,tran1]] #将原始列表中没有的词加到txt文件中 wri = word + '/' + tran1 dict.write(wri) translateReply.append(tran1) #flag = 1 #break else: translateReply.append(translation)if flag == 0: str1 = ' '.join(translateReply) if words[0] in ['what','where','why','how','is']: m='?' else: m='.' print(str1.capitalize()+m)#dictionary = [['the','la'],['red','resso'],['is',"e'"],['table','tavola']] def translateFromEnglish(word): lookup(word) return lookup(word) def lookup(word): for i in dictionary: if word == i[0]: #flag = 1 return i[1]def displayDictionary(): len_dic = len(dictionary) if len_dic == 0: print("*** The dictionary is empty ***") else: print("*** Known words ****") for i in range(len(dictionary)): a = dictionary[i] print(a[0].ljust(10),a[1].ljust(10))main()'''for i in range(len(dictionary)): wri = dictionary[i] wri = wri[0]+'/'+wri[1] if i < len(dictionary): wri = wri +'\n' dict.write(wri) ''' #dict.write(dictionary) dict.close()

print("姓名:小吴") print("学号:000000") print("班级:智能183班") print("step 6") print("--------------------")#从txt文件中获取字典dict = open('dictionary.txt','r+') dictionary = list(dict) #print(dictionary)#将读取到的内容排列成正常格式for i in range(len(dictionary)): dic1 = dictionary[i] flag = 0 for j in dic1: if j == '/': flag = 1 if flag == 0: raise Exception("程序运行错误") dic1 = dic1.strip('\n').split('/') #print(dic1) dictionary[i] = dic1#main()函数 def main(): global dictionary while True: flag = 0 reply = input("Eng: > ") if reply == '?': displayDictionary()#让程序可以正常结束 elif reply == '*save': dict.seek(0) dict.truncate() save1 = [] for i in range(len(dictionary)): wri = dictionary[i] save1 = save1 + [wri] wri = wri[0] + '/' + wri[1] if i < len(dictionary): wri = wri + '\n' dict.write(wri) elif reply == '*quit': fori in range(len(dictionary)): if dictionary[i] in save1: pass else: print(dictionary[i],'not in the dictionary,do you want to save it?') result = input("Yes or No?>") if result == 'Yes': wri = dictionary[i] wri = wri[0] + '/' + wri[1]+'\n' dict.write(wri) breakelse: translateReply = [] words = list(reply.strip('.,?').split(' ')) for word in words: translation = lookup(word.lower()) if translation == None: print('How do i translate',word,':') tran1=input() dictionary = dictionary+[[word,tran1]] translateReply.append(tran1) #flag = 1 #break else: translateReply.append(translation)if flag == 0: str1 = ' '.join(translateReply) if words[0] in ['what','where','why','how','is']: m='?' else: m='.' print(str1.capitalize()+m)#dictionary = [['the','la'],['red','resso'],['is',"e'"],['table','tavola']] def translateFromEnglish(word): lookup(word) return lookup(word) def lookup(word): for i in dictionary: if word == i[0]: #flag = 1 return i[1]def displayDictionary(): len_dic = len(dictionary) if len_dic == 0: print("*** The dictionary is empty ***") else: print("*** Known words ****") for i in range(len(dictionary)): a = dictionary[i] print(a[0].ljust(10),a[1].ljust(10))main()#dict.write(dictionary) dict.close()

【大作业|Python大作业一】水平有限,还请各位大佬批评指正~~

    推荐阅读