猜一封字母A-Z

时间:2018-06-08 16:19:12

标签: python edx

所以,这是任务。无法解决,请帮忙。

check_guess()需要2个字符串参数:letter和guess(都期望单个字母字符) - 如果猜测不是字母字符打印无效并返回False - 测试并打印,如果猜测是"高"或"低"并返回False - 测试并打印,如果猜测是"正确"并返回True

Letter Guess

创建letter_guess()函数,为用户提供3个猜测

为答案信取一个字母字符参数 获取用户输入的字母猜测 用回答和猜测来调用check_guess() 结束letter_guess如果 check_guess()等于True,返回True 或者在3次尝试失败后,返回False

首先,我无法解决3次尝试。第二个问题是,当我输入前的数字时,我无法犯错误。

letter = "J"
tries = 3

guess = input ("Enter your guess ")


def check_guess (guess, letter):


if letter == guess.upper():
    print ("correct")
    True
    return 

elif letter < guess.upper():
    print ("You are wrong, but go closer to A")
    False
    return 
elif letter > guess.upper():
    print ("You are wrong, but go closer to Z")
    False
    return 

def letter_guess (guess, letter, tries):
if check_guess (guess, letter) == True:
    pass
elif check_guess (guess, letter) == False:
    tries - 1
    return

if tries == 0:
print ("GAME OVER!")
else:
check_guess (guess, letter)

3 个答案:

答案 0 :(得分:1)

您没有使用letter_guess功能,并且您没有从None功能返回结果(check_guess除外)。
试试这个:

letter = "J"
tries = 3

def check_guess (guess, letter):
    if not guess.isalpha():
        print("Invalid")
        return False
    if letter == guess.upper():
        return True
    elif letter < guess.upper():
        print ("Your guess is High")
        return False
    else:
        print ("Your guess is Low")
        return False

def letter_guess():
    for i in range(tries):
        guess = input ("Enter your guess ")
        res = check_guess (guess, letter)
        if res:
            print ("Correct!")
            return True
    return False

result = letter_guess()
if result:
    print ("Congratulations")
else:
    print ("The answer was ",letter)
print ("GAME OVER!")

答案 1 :(得分:0)

通过删除letter_guess函数并将所有游戏逻辑压缩成一个函数,可以简化代码。

然后为了解决3次尝试的问题,我做了一个循环,循环直到尝试== 0.当循环命中一个猜测错误的语句时,它减去一次尝试。

我还必须移动

guess = input ("Enter your guess ")

进入while循环,因此它会多次迭代并获得三次不同尝试的输入。

correctLetter = "J"

def check_guess (letter):

    tries = 3

    while tries > 0:

        guess = input ("Enter your guess ")

        if letter == guess.upper():                
            print ("Correct!")
            break

        elif letter < guess.upper():
            if tries > 1:
                print ("You are wrong, but go closer to A")
            tries = tries - 1    #Moves you closer to finishing the loop

        elif letter > guess.upper():
            if tries > 1:
                print ("You are wrong, but go closer to Z")  
            tries = tries - 1  #Moves you closer to finishing the loop

    if tries == 0:
        print ("GAME OVER!")

check_guess(correctLetter)

附注:要从函数返回任何值,该值需要直接遵循return语句,如下所示:

return True

我希望有所帮助!

答案 2 :(得分:0)

这实现了相同的功能,但使用了两种不同的功能。

var credCache = new CredentialCache();
var basicCred = new NetworkCredential(htaccessUsername, htaccessPassword);
var digestCred = new NetworkCredential(username, apiKey);
credCache.Add(new Uri("http://.com/"), "Basic", basicCred);
credCache.Add(new Uri("http://.com/"), "Digest", digestCred);
var httpClient = new HttpClient(new HttpClientHandler { Credentials = credCache });

函数letter_game运行游戏,而letter_check函数返回一个布尔值,对应于猜测的字母是否与正确的字母匹配。