无法使用以前的函数创建python函数程序

can not create the python function programm using a previous function

这是我一开始要完成的任务。

猜字母A-Z。

check_guess()接受2个字符串参数:字母和猜测(都需要单个字母字符)。

  • 如果guess不是字母字符,则打印无效并返回false。

  • 如果猜测值为"高"或"低",则测试并打印,然后返回false

  • 测试并打印猜测是否"正确",并返回"真"?

我做了这个;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def check_guess(guess="input" , letter="g"):

    guess=input("enter input for guess:")

    if guess.isalpha()==False:

        print("invalid" , guess.isalpha())

    elif guess>letter:

       print("High")

    elif guess<letter:

       print("Low")

    else:

        print(guess==letter)

?现在我被要求这样做;

Letter Guess.

create letter_guess() function that gives user 3 guesses.

takes a letter character argument for the answer letter

gets user input for letter guess

calls check_guess() with answer and guess.

end letter_guess if check_guess() equals True, return True.

or after 3 failed attempts, return False.
?


我想你和我一样对这个平台很陌生,但是你必须更清楚地组织你的问题,提出具体的问题,而不是要求我们解决你的问题。你在挣扎什么?我的意思是,您已经在文本中编写了您需要的东西,现在您所要做的就是在Python中研究相应的解决方案。你需要的基本上是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# function to determine matches

def ismatch(letter, guess):
    if (letter == guess):
        return True
    else:
        return False

# function to determine if input is valid (do some research or write your
# own by storing all valid characters somehow and checking if input is in
# it, check: # https://stackoverflow.com/questions/15558392/how-to-check-if-
# character-in-string-is-a-letter-python

def isalpha(guess):
    if (guess.isalpha()):
        return True
    else:
        return False

# Main programm (check how to use loops)
# create a counter for amount of guesses
# save the input for the letter to be guessed after checking if it's valid
# otherwise restart input for the letter (or exit program if you like)
# save the input of the guess after checking if it's valid
# otherwise restart input for the guess(or exit program if you like)
# compare if guess was right with your match-function
# if so, finish programm
# otherwise raise the guess_counter by 1
# if limit of guess counter ( in your case 3) reached, exit program


您应该使用raw_input将用户输入读取为字符串。根据您的要求,您可以尝试此代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def check_guess(guess="a", letter="g"):
    guess = raw_input("enter input for guess:")
    if guess.isalpha()==False:
        print("invalid" , guess.isalpha())
    elif guess>letter:
       print("High")
    elif guess<letter:
       print("Low")
    else:
        print(guess==letter)
        return True
    return False

def letter_guess():
    input_letter = raw_input("Enter the Answer letter for match:")
    for x in range(3):
        status = check_guess(letter=input_letter)
        if status is True:
            return True
    return False

if __name__ == '__main__':
    letter_guess()

希望这有帮助!