猪拉丁语功能句子(Pig Latin Function Sentence)

编程入门 行业动态 更新时间:2024-10-23 01:42:05
拉丁语功能句子(Pig Latin Function Sentence)

拉丁语句子的规则是:

1.对于以辅音开头的单词,第一个元音之前的所有字母都移动到该单词的末尾,并进一步附加'ay' 2.对于以元音开始的单词,该单词附有'干草' 3.对于不包含元音的词,第一个字母移动到单词的末尾,并附有'方式' 4.所有非字符(即数字,符号)都被忽略

代码的主要部分执行一个无限循环,该循环接受来自用户的字符串输入,调用pigLatin函数并打印返回的结果。 pigLatin函数接收一个单词列表作为其输入,并使用上述规则将它们转换为拉丁文。 当用户输入“退出该程序”作为转换的输入句子时,主要部分退出。 验证所有情况下的程序(包括无效输入)并保存程序。

我觉得很难解决规则3.当我测试代码时,输​​出是不正确的......我整晚都在努力争取,并且无法入睡....请帮助这里是我的代码:

enter code here

sentence=input('input:') VOWELS=['a','e','i','o','u'] CONSONANTS=['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'] def pigLatin(): t=-1 for word in newsentence: if word[0] in CONSONANTS: if 'a'and'e'and'i'and'o'and'u' not in word[1:]: print(word[1:]+word[0]+'way',end=' ') for u in word: t +=1 if u in VOWELS: print (word[t:]+word[0:t]+'ay',end=' ') break elif word[0] in VOWELS: print (word[0:]+'hay',end=' ') else: print (word[0:]) while sentence!=('Quit the program'): newsentence=sentence.split() pigLatin() sentence=input('input:')

THe rules for the pig latin sentences are:

1.for words beginning with consonants, all letters before the first vowel are moved to the end of the word, and is further appended by 'ay' 2.for words that begin with vowels, the word is appended by 'hay' 3.for words that do not contain a vowel, the first letter is moved to the end of the word and is appended with 'way' 4. all non characters (i.e., numbers, symbols) are ignored

The main part of the code executes an infinite loop that takes a string input from the user, invokes the pigLatin function and prints the result returned. The pigLatin function receives a list of words as its input and converts them to pig latin using the rules above. The main part exits when the user input "Quit this program" as the input sentence for conversion. Verify your program for all cases (including invalid inputs) and save your program.

I find it very difficult to solve rule 3. As I test the code, the out put isn't correct... I struggle it for the whole night and couldn't sleep....Please help Here is my code:

enter code here

sentence=input('input:') VOWELS=['a','e','i','o','u'] CONSONANTS=['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'] def pigLatin(): t=-1 for word in newsentence: if word[0] in CONSONANTS: if 'a'and'e'and'i'and'o'and'u' not in word[1:]: print(word[1:]+word[0]+'way',end=' ') for u in word: t +=1 if u in VOWELS: print (word[t:]+word[0:t]+'ay',end=' ') break elif word[0] in VOWELS: print (word[0:]+'hay',end=' ') else: print (word[0:]) while sentence!=('Quit the program'): newsentence=sentence.split() pigLatin() sentence=input('input:')

最满意答案

正如Willem Van Onsem所说,这条线不符合你的想法:

if 'a'and'e'and'i'and'o'and'u' not in word[1:]:

你应该尝试尝试解释器。

'a' and 'e' not in "bar" # returns True

发生这种情况是因为它是一个逻辑运算符,它会查看前后的内容。 它是这样做的:

'a' and ('e' not in "bar")

评估为

'a' and True

它的计算结果为True,因为非空字符串被赋值为布尔值True。

这是可行的。 它使用all函数,如果它的所有元素都是真的,它将只返回True,而Python的一个特性叫做comprehension,它允许你做整洁的循环:

vowels = "aeiou" all(char not in vowels for char in word)

As Willem Van Onsem said, this line doesn't do what you think:

if 'a'and'e'and'i'and'o'and'u' not in word[1:]:

You should try experimenting with the interpreter.

'a' and 'e' not in "bar" # returns True

This is happening because and is a logical operator which looks at what comes before and after it. It is doing this:

'a' and ('e' not in "bar")

which evaluates to

'a' and True

which evaluates to True, because non-empty strings get assigned the boolean True.

Here is something that will work. It uses the all function, which only returns True if all of its elements are true, and a feature of Python called comprehension which allows you to do tidy loops:

vowels = "aeiou" all(char not in vowels for char in word)

更多推荐

本文发布于:2023-07-17 07:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:拉丁语   句子   功能   Pig   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!