練習題2

  1. 設計一個程序,從終端接收10個數字,並使用本身編寫的排序函數,對10個數字排序後輸出

def numberSort():python

list1 = []

Flag = 10

while Flag:

    n = input('pelase input number:')

    Flag-=1

    list1.append(n)

list1.sort()

return list1

print numberSort()app

  1. 設計一個函數,接收一個英文單詞,從文件中查詢該單詞的漢語意思並返回

def englishiIntoChinese():ide

word = raw_input('please input english word:')函數

with open('dictfile.txt', 'r') as f:設計

for i in f:

       if word==i.strip().split()[0]:

           return i.strip().split()[1]

   return '不存在!'

print englishiIntoChinese()code

字典的方法:排序

def englishiIntoChinese():ip

dict1 = {}

# list1=[]

word = raw_input('please input english word:')

with open('dictfile.txt', 'r') as f:

    for i in f:

        if word in i:

            #dict1.setdefault(i.strip().split(0),i.strip().split(1))

            #data = i.strip().split()

            dict1.setdefault(i.strip().split()[0],i.strip().split()[1])

            break

    for k in dict1:

        if word ==k:

            print dict1[k]

englishiIntoChinese()utf-8

3,input

#!/usr/bin/env python

#-- coding:utf-8 --

word = raw_input('please input english word:')

with open('dictfile.txt', 'r') as f:

for i in f:

    if cmp(i[0], word[0]) == 1:

            print '不存在'

            break

    elif word == i.strip().split()[0]:

            print i.strip().split()[1]

            break

    else:

            continue
相關文章
相關標籤/搜索