It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                                7年前关闭。
            
                    
问题

guess_letter:
(str, str) -> str


第一个参数是未猜测的辅音,第二个参数是难度。返回计算机播放器接下来要猜测的辅音。如果level为EASY,则随机选择要猜测的辅音。如果level为HARD,则辅音猜测第一个参数中出现的PRIORITY_CONSONANTS中的第一个辅音。

PRIORITY_CONSONANTS = 'tnrslhdcmpfgybwvkqxjz'
EASY = 'E'
HARD = 'H'


总体而言,该项目将创建一个功能强大的Hangman游戏,这是我必须自己编写的一个组件。

我不知道该怎么写,我做了一个我想解决的小结构。但老实说,我不明白该写些什么。

我希望当难度级别困难时,可以选择优先级辅音中出现在参数未猜测辅音中的第一个辅音
当容易的时候,我希望任何随机辅音出现

guess_letters(unguessed_consonants, difficulty_level):
if difficulty_level == EASY:
if difficulty_level ==HARD:

最佳答案

import random

EASY = 'E'
HARD = 'H'
PRIORITY_CONSONANTS = 'tnrslhdcmpfgybwvkqxjz'
EASY_CONSONANTS = '' # what should be here?

def guess_letters(unguessed_consonants, difficulty_level):
    if difficulty_level == EASY:
        search_set = EASY_CONSONANTS
    elif difficulty_level == HARD:
        search_set = PRIORITY_CONSONANTS

    for con in unguessed_consonants:
        if con in search_set:
             return con


针对Nadia的评论:


  kk效果更好,除非如果我用力输入'zk'会给我z而不是k k在PRIORITY_CONSONANTS中的z之前,所以输出应该是k


在这种情况下,应该首先对search_set进行迭代:

    for con in search_set:
        if con in unguessed_consonants:
             return con

关于python - Python中的Hangman-辅音猜测编码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12980304/

10-12 21:14