| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1241 人关注过本帖
标题:关于Pig Latin 翻译器编制问题(Python 3 Problem)
只看楼主 加入收藏
qingmui
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-10-6
收藏
 问题点数:0 回复次数:0 
关于Pig Latin 翻译器编制问题(Python 3 Problem)
老师叫我们做一个英文变Pig Latin 的翻译器,Pig latin 就是,如果一个单词是元音开始的,只要加上 "way" 在单词未就可以了. 比如, "apple" becomes "appleway", "ear" becomes "earway", and "octopus" becomes "octopusway".

如果一个单词是由非元音开始,那么第一个字母移到最末位然后加"ay". For example, "dog" because "ogday", "computer" becomes "omputercay", and "data" becomes "ataday".

如果一个单词石油非元音开始,而且继续下去(直到第一个元音字母) --那么就把非元音的全部移到元音的后面. For example, "chair" becomes "airchay", "trouble" becomes "oubletray", and "street" becomes "eetstray".

暂时不需要担心 q's and u's or 其他特殊情况
这里的元音字母指“ "a", "e", "i", "o" or "u"”

==要求==
Function 1: pigWord. This function must take a single word as a parameter and return its Pig Latin translation. The function may assume the following:

The parameter will be a non-empty string (length at least 1)
All the characters in the string will be letters
The first character may be upper- or lower-case. The rest of the characters will be lower-case.
The word will include at least one vowel.
If the parameter is capitalized, your function should return a capitalized result. If it's not capitalized, the result should not be capitalized either. For example, pigWord("cat") should return "atcay" but pigWord("Cat") should return "Atcay".
This function should not do any I/O. You should not try to read a word or print the result. Just use the parameter and return the result. If you want to print some values as "debug output" while you're testing your program, feel free, but remove it (or comment it out) before you hand it in. The only exception is if you're unable to get your function working. In that case, you may leave in a small amount of debug output to demonstrate to the marker that some of your function is working correctly. If you do this, make sure to include comments to help the marker understand the output. If they don't understand it, they will ignore it.

Function 2: pigSentence. This function must take a whole sentence as a parameter and return its Pig Latin translation. Obviously, it will call pigWord to translate each individual word in the sentence. The challenge of this function is to separate the sentence into individual words and combine the individual translated words into a new sentence. Your initial version of this function may assume that:

The sentence will consist of one or more words (each consisting of letters but no other characters).
The last word will be followed by a period (".") and there will be no more characters after the period.
Each word except the last may be followed by a comma. There will be no spaces between the word and the comma after it.
Each word except the last will be followed by one or more spaces (after the optional comma).
There will be no characters in the sentence except letters, spaces, comma, and the ending period.
Every character in a word except possibly the first character will be in lower-case.
Multiple spaces in the parameter should be replaced by single spaces in the result.
Error Handling: If you are producing a user-friendly Pig Latin translation program, it should not make any assumptions about its input. Instead, it should detect errors and write meaningful error messages in response.

To keep this assignment from getting too big, I'm not going to ask you to detect all possible errors. But I do want you to practice a little bit of error checking. Once you've got pigSentence working, you should modify it so that it catches the following two kinds of errors:

The parameter does not contain a period at the end
The parameter contains more than one period
If either of these errors occurs, your function should write an informative error message (not just "error"!) and return an empty string ("").

===================样版例子输出============================
Test Run: The following is a transcript using my sample solution:


>>> pigSentence("I came, I saw, I conquered.")
'Iway amecay, Iway awsay, Iway onqueredcay.'
>>> pigSentence("     Fred         and          George      are    twins.")
'Edfray andway Eorgegay areway instway.'
>>> pigSentence("  not  a sentence    ")
error: sentence does not end with a period
''
>>> pigSentence("period. in the wrong place")
error: sentence does not end with a period
''
>>> pigSentence("  two. many. periods.")
error: multiple periods in sentence
''
>>>        

================问题:我做了个如下的coding=======================
vowels = 'AaEeIiOoUu'

def main():
    sent = input("Please enter the text to be translated: ")
    print(pigSentence(sent))


def pigWord(sent):
    for word in sent.split():
        if word[0] in vowels:
            print(word[0]+word[1:] + 'way')
        elif word[0] not in vowels and word[1] in vowels:
            print(word[1:]+word[0]+'ay')
        elif word[i] in vowels and i >= 2:
            print(word[i:]+word[:i]+'ay')
        else:
            pass
        

def pigSentence(sent):

==============================

在pigWord中,我不知道怎么ruturn 只有一行的输出,也不知道如何改变逗号序列问题。 也不知道如何保证第一个字母是大写的,于是想问问大家。
搜索更多相关主题的帖子: computer 翻译器 apple 英文 老师 
2011-01-29 09:47
快速回复:关于Pig Latin 翻译器编制问题(Python 3 Problem)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022152 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved