繁体   English   中英

字符串中特定字母的数量

[英]Number of specific letter in string

我正在尝试制作一个刽子手游戏(只有 6 个字母的单词),并且我正在尝试为单词中超过 1 个特定字母(由用户输入)编写代码

    tries = 0
    n = 0
    word = random.choice(word_list)
    print(word)
    while  tries<10:
        guess = input("Input a letter: ")
        if guess in word:
            n = n + 1
            print("Correct. You've got,", n,"out of 6 letters.")
            if n == 6:
                print("You guessed correctly, the word was,", word)
                break
        elif word.guess(2):
            n = n + 2
            print("Correct. You've got,", n,"out of 6 letters.")
            if n == 6:
                print("You guessed correctly, the word was,", word)
                break

尽管在输入双字母进行猜测后程序继续(例如,'s' in 'across')它仍然没有在'n'变量中加起来正确的数字

您可以为此使用count() 用它来获取单词中出现的次数。 如果 word 中不存在字符,则返回 0。 就像在input()之后的while内一样 -

c = word.count(guess)
if c:
    n += c
    if n == 6:
        print("You guessed correctly, the word was,", word)
        break
    print("Correct. You've got,", n, " out of 6 letters.")

您可能想要检查用户输入是否确实是字符而不是字符串或'' (空字符串)。 这可能会混淆程序。 此外,您不会增加tries变量。 因此,用户可以无限次尝试

另外,您的代码中的word.guess(2)是什么。 那是错字吗?

您还需要删除已经猜到的字母。 您可以使用替换 function 来执行此操作。 像这样:

tries = 0
n = 0
word = random.choice(word_list)
word_updated = word
print(word)
while  tries<10:
    if n == 6:
        print("You guessed correctly, the word was,", word)
        break
    guess = input("Input a letter: ")
    if guess in word:
        n += word_updated.count(guess)
        word_updated = word_updated.replace(guess, "")
        print("Correct. You've got,", n,"out of 6 letters.")

我冒昧地实施了这个

import random          

from collections import Counter

attempt = 0
tries = 10
correct = 0
character_to_check = 6
word_list = ['hello','hiedaw','rusiaa','canada']
word = list(random.choice(word_list))
print(word)
dic = dict(Counter(word))
while attempt <= tries:
    if correct==character_to_check :
        print("You guessed correctly, the word was {}".format(word))
        correct = 0 

        break

    guess = str(input("enter a letter "))
    if len(guess)>1 or len(guess)==0:
        print("only 1 letter")
    else:
        if guess in word:
           if dic[guess]>0: 
               correct += 1
               dic[guess]-=1
               print("you have guessed correct, you got {} out of {} letter".format(correct, character_to_check))
           else:
               print("already guessed this character")
    attempt+=1
    if attempt>tries:
        print("attempt exceed allowed limit")

output

['r', 'u', 's', 'i', 'a', 'a']

enter a letter 'r'
you have guessed correct, you got 1 out of 6 letter

enter a letter 'r'
already guessed this character

enter a letter 'u'
you have guessed correct, you got 2 out of 6 letter

enter a letter 's'
you have guessed correct, you got 3 out of 6 letter

enter a letter 'i'
you have guessed correct, you got 4 out of 6 letter

enter a letter 'a'
you have guessed correct, you got 5 out of 6 letter

enter a letter 'a'
you have guessed correct, you got 6 out of 6 letter
You guessed correctly, the word was ['r', 'u', 's', 'i', 'a', 'a']

一个有趣且难以理解的单行代码可以做到这一点:

print(globals().update({"word": __import__("random").choice(['collection','containing','random','words']), "checked" : set(), "f" : (lambda :  f.__dict__.update({"letter" : input("Input a letter:\n")}) or ((checked.add(f.letter) or (print("Good guess!") if f.letter in word else print("Bad guess..."))) if f.letter not in checked else print("Already checked...")) or (print("You guessed correctly the word " + word + "!") if all(l in checked for l in word) else (print("Too many attempts... You failed!") if len(checked) > 10 else f())))}) or f() or "Play again later!")

(可能不要在生产代码中使用)

我正在使用一set来记住尝试的字母和尝试次数,不需要另一个变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM