给定尝试次数后如何终止此循环?

时间:2019-10-17 00:40:57

标签: python

在大多数情况下,它似乎正在工作,但我没有设法使其正常工作。当我运行它时,我可以无限制地尝试猜测单词中的每个字母,直到我拼出单词为止。

但这不是我要的,我试图给用户5个单字母猜测,如果字母在单词中,那么它将告诉他们“是”,有一个(n)(用户猜测),但是如果字母不在我的单词中,它将告诉他们“否”,那么我的单词中就不会有(使用者猜测)。

5次尝试猜测不同的字母后,我希望他们必须猜测完整的单词,但我不知道怎么做。

这就是我现在拥有的:

import random

def get_word():
    words = ['cat', 'dog', 'man', 'fox', 'jar']
    return random.choice(words).upper()

def check(word,guesses,guess):
    status = ''
    matches = 0
    for letter in word:
        if letter in guesses:
            status += letter
        else:
            status += '*'
        if letter == guess:
            matches += 1

    count = 0
    limit = 5

    if matches > 1:
        print ('Yes! there is a(n)',guess,' in my word.')
        guesses += guess
    elif matches ==1:
        print ('Yes! there is a(n)',guess,' in my word.')
        guesses += guess
        while count > limit:
            input('What do you think my word is')
    else:
        print('No, there is not a(n)',guess,' in my word.')
        guesses += guess
        while count > limit:
            input('What do you think my word is')

    return status


def main():
    word = get_word()
    guesses = ""
    guessed = False
    print ('I am thinking of a 3 letter word with no repeating letters. You get five guesses of the letters in my word and then you have to guess the word.')
    while not guessed:
        text = 'Guess a letter in my word:'
        guess = input(text)
        guess = guess.upper()
        count = 0
        limit = 5
        if guess in guesses:
            print ('You already guessed "' + guess + '"')
        elif len(guess) == len(word):
            guesses += guess
            if guess == word:
                guessed = True
            else:
                print('No, there is not a(n) "' + guess + '"')
        elif len(guess) == 1:
            guesses += guess
            result = check(word,guesses,guess)
            if result == word:
                guessed = True

            else:
                print (result)

        else:
            print ('Invalid entry.')

    print ('Yes! you correctly guessed')

main()

2 个答案:

答案 0 :(得分:0)

对于初学者来说,while not guessed:将一直持续到猜测为真->单词被猜测为止。接下来,如果您希望有5个猜测,那么您想for i in range(0, 5):做一个答案猜测,然后运行猜测逻辑替换

if result == word:
    guessed = True

使用

if result == word:
    guessed = true
    break

根据正确的猜测跳出循环。然后,要允许在循环之外进行猜测,请检查是否已经猜测到,如果不能则进行猜测。

此外,您还应检查它们输入的字符是否与

类似
guess = input(text)
while len(guess) != 1:
    guess = input(text)

答案 1 :(得分:0)

我主要尝试保留您的原始代码和思路。我所做的主要更改是摆脱了检查功能,因为我觉得它没有任何用处。我也将猜测变量更改为列表,并利用其属性进行评估。

import random

def get_word():
    words = ['cat', 'dog', 'man', 'fox', 'jar']
    return random.choice(words).upper()

def main():
    word = get_word()
    guesses = []
    guessed = False
    print('I am thinking of a 3 letter word with no repeating letters.'
    ' You get five guesses of the letters in my word and then you have'
    ' to guess the word.\n')
    while not guessed and len(guesses)<5:
        text = 'Guess a letter in my word:\n'
        guess = input(text)
        guess = guess.upper()
        if guess in guesses:
            print ('You already guessed "', guess, '"\n')
        elif len(guess) > 1:
            guesses.append(guess)
            if guess == word:
                guessed = True
            else:
                print('No, ', guess, 'is not the word!\n')
        elif len(guess) == 1:
            guesses.append(guess)
            if guess in word:
                print('Yes there is a(n) ', guess, 'in the word!\n')
            else:
                print('No, there is not a(n)', guess, 'in the word!\n')
        else:
            print ('Invalid entry.\n')
    if guessed:
        print ('You correctly guessed the word!\n')
    else:
        correct_guesses = [guess for guess in guesses if guess in word]
        print('Last chance, what is the full word? (HINT: You have'
        'correctly guessed ', str(correct_guesses), ')"')
        fword = input()
        if fword.upper() == word:
            print('You correctly guessed the word!\n')
        else: print('Tough luck! You are out of chances! The correct'
        'word was ', word, '\n')

main()

playing = True
while playing:
    print('Would you like to play again?')
    answ = input()
    if answ.upper() == 'YES':
        main()
    else:
        print('Thank you for playing.')
        playing = False