小编Abi*_*ilB的帖子

计算单词中的音节数

我是初学者,我有一个需要帮助的问题.它是功课,所以任何提示都受到赞赏.我看过一些类似的话题,但答案超出了我所知道的怎么做......

我需要计算文本文件中音节的数量,作为更大程序的一部分.除了音节,我有我需要的一切.我尝试了几种不同的方法,但它并不总能捕捉特殊情况.我应该'计算相邻元音的组,不包括单词末尾的'e'.我明白这意味着什么,但我无法在我的计划中做到这一点.这是我的:::

def syllables(word):
    syl = 0
    vowels = 'aeiouy'
    starts = ['ou','ei','ae','ea','eu','oi']
    endings = ['es','ed','e']
    word = word.lower().strip(".:;?!")
    for vowel in vowels:
        syl +=word.count(vowel)
    for ending in endings:
        if word.endswith(ending):
            syl -=1
    for start in starts:
        if word.startswith(start):
            syl -=1
    if word.endswith('le'):
        syl +=1
    if syl == 0:
        syl+=1
    return syl
Run Code Online (Sandbox Code Playgroud)

编辑:新代码

def syllables(word):
    count = 0
    vowels = 'aeiouy'
    word = word.lower().strip(".:;?!")
    if word[0] in vowels:
        count +=1
    for index in range(1,len(word)):
        if word[index] in vowels and word[index-1] …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

7
推荐指数
1
解决办法
1万
查看次数

Python 3对元组列表进行排序?

我是Python新手,我有一个问题。有人告诉我要问这个问题,与我为同一程序编写的另一个帖子分开问。这是家庭作业,所以我只需要一些指导。我有一个元组列表,我想按tuple [0]对其进行排序,然后将要打印的完整元组返回到屏幕。元组由(得分,标记(x或o),索引)组成

这是我的基本代码(井字游戏的一部分-我在另一篇文章中有完整代码):::

listOfScores = miniMax(gameBoard)
best = max(listOfScores, key=lambda x: x[0])

if best[0] == 0:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will lead to a tie.")
        elif best[0] > 0:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will lead to a win.")
        else:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will …
Run Code Online (Sandbox Code Playgroud)

python sorting list

3
推荐指数
1
解决办法
1万
查看次数

Python 3 NoneType是不可订阅的错误

我遇到了这个错误的严重问题.这是家庭作业,所以我只是在寻找一些关于这个的提示...我已经看了其他问题,但他们没有帮助,所以希望这没关系.我是Python的新手.:)我正在做一个Tic Tac Toe游戏.我确信它还有其他一些问题,但我现在主要担心的是这个错误.这是我的代码:::

    scoreList = []
"""keeps track of all of the scores, moves, and indexes as tuples for each
        winning board"""

def getEmptySpaces(gameBoard):
    return gameBoard.count('-')

def winner(gameBoard):
    case1 = gameBoard[0]+gameBoard[1]+gameBoard[2]
    case2 = gameBoard[0]+gameBoard[3]+gameBoard[6]
    case3 = gameBoard[0]+gameBoard[4]+gameBoard[8]
    case4 = gameBoard[1]+gameBoard[4]+gameBoard[7]
    case5 = gameBoard[2]+gameBoard[5]+gameBoard[8]
    case6 = gameBoard[2]+gameBoard[4]+gameBoard[6]
    case7 = gameBoard[3]+gameBoard[4]+gameBoard[5]
    case8 = gameBoard[6]+gameBoard[7]+gameBoard[8]

    if case1 == 'xxx' or case1 == 'ooo':
        return True
    elif case2 == 'xxx' or case2 == 'ooo':
        return True
    elif case3 == 'xxx' or case3 == 'ooo': …
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
936
查看次数

标签 统计

python ×3

list ×1

python-3.x ×1

sorting ×1