【问题标题】:In this assignment you will ask the user to input an English word, translate that word into ”Pig Latin” and then print both words在这个作业中,您将要求用户输入一个英文单词,将该单词翻译成“Pig Latin”,然后打印这两个单词
【发布时间】:2019-03-25 09:50:17
【问题描述】:

我的计算机科学课有这个作业要交:

在此作业中,您将要求用户输入一个英文单词,将该单词翻译成“Pig Latin”,然后打印这两个单词。

这些是规则:

  1. 如果一个词以元音开头(a, A, e, E, i, I, o, O, u, U),那么翻译是通过在结尾添加一个“方式”来形成的单词。例如“at”变成“atway”,“egg”变成“eggway”

  2. 如果一个词不包含元音(a、A、e、E、i、I、o、O、u、U),则翻译是通过在词尾添加“方式”来形成的.例如“my”变成“myway”,“by”变成“byway”

  3. 如果单词以辅音开头并包含元音,则通过将辅音向上移动到单词末尾的第一个元音并添加“ay”来形成翻译。例如“bat”变成“atbay”,“that”变成“atthay”,“three”变成“eethray”

  4. 以首字母大写开头的单词应翻译成首字母大写的单词。例如“休斯顿”变成了“Oustonhay”,“冰岛”变成了“Icelandway”,“Marry”变成了“Arrymay”

这是我到目前为止的编程。我被第三条和第四条卡住了:

def is_vowel(letter):
    return letter.upper() in 'AEIOU'

def has_vowel(word):
    for letter in word:
        if is_vowel(letter):
            return True 
    return False

def translate(word):
    if is_vowel(word[0]): #is the first letter
        return word + "way"
    elif has_vowel(word):
        pass
    else: #there is no vowel
        return word + "way"

#stuff before the loop
print("This program will translate a word from English to Pig Latin.")
choice = "Y"

#stuff in the loop
while choice.upper() == 'Y':
    word = input("Please enter a word: ")
    print(word, "becomes", translate(word) + ".")
    choice = input("Would you like another word? (Y/N) ")

#stuff after the loop
print("Ankthay ouyay!")

我知道这是对的,因为我们在课堂上做了这部分,但当我回到家时,我完全迷失了,不知道我需要做什么!请帮忙!

【问题讨论】:

  • 首先,您的缩进是错误的(例如在您的ifelif 之后),这在Python 中很重要。请修复它。
  • 刚刚更新了!

标签: python


【解决方案1】:

第三条规则要求你找到第一个元音的索引,然后翻转周围的字符。有几种方法可以找到第一个元音的索引,但我认为这是你最容易理解的方法:

def get_vowel_index(word):
    for i, letter in enumerate(word):  # use enumerate() to iterate over index, value pairs
        if letter.upper() in 'AEIOU':
            return i  # return the index

现在您可以使用此函数查找元音索引,然后对单词进行切片,使其符合您的第三条规则,例如:

word = "that"
index = get_vowel_index(word)
pl_word = word[index:] + word[:index] + "ay"
print(pl_word)  # atthay

您甚至可以使用此函数代替其他函数,因为如果单词中没有元音或索引中没有元音,它将返回None,因此通过简单检查此函数的返回,您可以检查所有三个状况。这为您提供了一种优化代码的方法,因为规则是独占的,因此您可以将 translate() 函数重写为:

def translate(word):
    vowel_index = get_vowel_index(word)  # get the vowel index
    # the rule for the vowel as the first letter and no vowels is the same:
    if vowel_index == 0 or vowel_index is None:
        return word + "way"
    # otherwise there is a vowel in the word, flip the consonants to the end:
    return word[vowel_index:] + word[:vowel_index] + "ay"

你可以很容易地测试它:

for v in ("at", "egg", "my", "by", "bat", "that", "three"):
    print(translate(v))

这将产生:

在途
蛋道
我的方式
小路
在海湾
阿泰
eethray

至于第四条规则,您只需要检查第三个条件中的第一个字母是否为大写(因为前两个字母保持不变)并将其转换为标题(检查str.title())如果是.因此,要完成translation() 功能,您可以执行以下操作:

def translate(word):
    index = get_vowel_index(word)  # get the vowel index
    if index == 0 or index is None:
        return word + "way"
    # otherwise there is a vowel in the word, flip the consonants to the end
    if word[0].isupper():  # the first letter was uppercase
        return (word[index:] + word[:index] + "ay").title()  # title-case it!
    return word[index:] + word[:index] + "ay"

并对其进行测试:

for v in ("Houston", "Iceland", "Marry"):
    print(translate(v))

产量:

奥斯顿海
冰岛之路
阿利梅

【讨论】:

    【解决方案2】:

    由于这是一种家庭作业,我不是编写代码(SO 上值得称赞的做法)。

    规则 3 应该包括找出哪个字符是第一个元音,然后将单词的片段从开头移动到该字符到单词的结尾(从技术上讲,您可以用两个片段替换字符串,因为字符串是不可变的在python中),然后附加“ay”

    规则 4:如果单词的第一个字母的大写(在任何修改之前)与该字母相同,则将该信息保存到某个变量中并基于该更改(或不更改)输出的第一个字母

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2022-01-15
      • 2016-01-13
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多