
高分悬赏python问题!好的高分追加!
帮个忙吧,好的话加分,这个程序忙帮做出来吧***PUTYOURALGORITHMHERE***#GivensomewordsineitherEnglishorItalia...
帮个忙吧,好的话加分,这个程序忙帮做出来吧
*** PUT YOUR ALGORITHM HERE ***
# Given some words in either English or Italian convert them to the other language
# Return the converted words.
def translator(language, word):
# create the dictionaries from the given files
# check if the word is in the relevant dictionary
# convert to either Italian or Spanish
# check if translation is same in both Italian and Spanish
# print out a message, e.g.
# The Spanish word for sun is sol
# The Italian word for rubbish is not found
# The Spanish word for great is grande - same in both Italian and Spanish.
# Run the tests if this is the main program:
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
文件: english to spanish
good bueno
afternoon tarde
cat gato
dog perro
white blanco
finish final
exit salida
fever fiebre
fox zorro
jump salto
lazy perezoso
fish pescados
not no
under debajo
pretty bastante
slow lento
sun sol
run funcionamiento
painting pintura
beautiful hermoso
great grande
flower flor
house casa
english to italian
good buono
afternoon pomeriggio
cat gatto
dog cane
white bianco
finish fine
exit uscita
fever febbre
fox volpe
jump salto
lazy pigro
fish pesci
not non
under sotto
pretty carino
slow lento
sun sole
run corsa
painting quandro
beautiful bello
great grande
flower fiore
clever intelligente
house casa
########## END AUTOMATIC TESTING ##################################### 展开
*** PUT YOUR ALGORITHM HERE ***
# Given some words in either English or Italian convert them to the other language
# Return the converted words.
def translator(language, word):
# create the dictionaries from the given files
# check if the word is in the relevant dictionary
# convert to either Italian or Spanish
# check if translation is same in both Italian and Spanish
# print out a message, e.g.
# The Spanish word for sun is sol
# The Italian word for rubbish is not found
# The Spanish word for great is grande - same in both Italian and Spanish.
# Run the tests if this is the main program:
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
文件: english to spanish
good bueno
afternoon tarde
cat gato
dog perro
white blanco
finish final
exit salida
fever fiebre
fox zorro
jump salto
lazy perezoso
fish pescados
not no
under debajo
pretty bastante
slow lento
sun sol
run funcionamiento
painting pintura
beautiful hermoso
great grande
flower flor
house casa
english to italian
good buono
afternoon pomeriggio
cat gatto
dog cane
white bianco
finish fine
exit uscita
fever febbre
fox volpe
jump salto
lazy pigro
fish pesci
not non
under sotto
pretty carino
slow lento
sun sole
run corsa
painting quandro
beautiful bello
great grande
flower fiore
clever intelligente
house casa
########## END AUTOMATIC TESTING ##################################### 展开
1个回答
展开全部
# -*- coding: utf-8 -*-
#*** PUT YOUR ALGORITHM HERE ***
# Given some words in either English or Italian convert them to the other language
# Return the converted words.
def translator(language, word):
# create the dictionaries from the given files
english2spanish = {}
english2italian = {}
file1 = open('english2spanish.txt').readlines()
file2 = open('english2italian.txt').readlines()
for theline in file1:
words = theline.strip().split(' ')
english2spanish[words[0]]= words[1]
for theline in file2:
words = theline.strip().split(' ')
english2italian[words[0]]= words[1]
# check if the word is in the relevant dictionary
if word in english2spanish:
pass
else:
return 'Word not found'
# convert to either Italian or Spanish
word1, word2 = english2spanish[word], english2italian[word]
# check if translation is same in both Italian and Spanish
sign = 0
if word1 == word2:
sign = 1
if language == 'spanish' or language == 'Spanish':
if sign:
print 'The Spanish word for '+word+' is '+word1+' - same in both Italian and Spanish.'
else:
print 'The Spanish word for '+word+' is '+word1
if language == 'italian' or language == 'Italian':
if sign:
print 'The Italian word for '+word+' is '+word2+' - same in both Italian and Spanish.'
else:
print 'The Italian word for '+word+' is '+word2
# print out a message, e.g.
# The Spanish word for sun is sol
# The Italian word for rubbish is not found
# The Spanish word for great is grande - same in both Italian and Spanish.
# Run the tests if this is the main program:
if __name__ == '__main__':
translator('spanish','great')
#import doctest
#doctest.testmod(verbose=True)
两个文件分别命名为
english2spanish.txt
english2italian.txt
#*** PUT YOUR ALGORITHM HERE ***
# Given some words in either English or Italian convert them to the other language
# Return the converted words.
def translator(language, word):
# create the dictionaries from the given files
english2spanish = {}
english2italian = {}
file1 = open('english2spanish.txt').readlines()
file2 = open('english2italian.txt').readlines()
for theline in file1:
words = theline.strip().split(' ')
english2spanish[words[0]]= words[1]
for theline in file2:
words = theline.strip().split(' ')
english2italian[words[0]]= words[1]
# check if the word is in the relevant dictionary
if word in english2spanish:
pass
else:
return 'Word not found'
# convert to either Italian or Spanish
word1, word2 = english2spanish[word], english2italian[word]
# check if translation is same in both Italian and Spanish
sign = 0
if word1 == word2:
sign = 1
if language == 'spanish' or language == 'Spanish':
if sign:
print 'The Spanish word for '+word+' is '+word1+' - same in both Italian and Spanish.'
else:
print 'The Spanish word for '+word+' is '+word1
if language == 'italian' or language == 'Italian':
if sign:
print 'The Italian word for '+word+' is '+word2+' - same in both Italian and Spanish.'
else:
print 'The Italian word for '+word+' is '+word2
# print out a message, e.g.
# The Spanish word for sun is sol
# The Italian word for rubbish is not found
# The Spanish word for great is grande - same in both Italian and Spanish.
# Run the tests if this is the main program:
if __name__ == '__main__':
translator('spanish','great')
#import doctest
#doctest.testmod(verbose=True)
两个文件分别命名为
english2spanish.txt
english2italian.txt
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询