刽子手游戏替换字母

2024-06-16 19:03:58 发布

您现在位置:Python中文网/ 问答频道 /正文

graphics=['''------------''',                 
'''------------
|         |  ''',

'''------------
|         |         
|          O''',
'''------------
|         | 
|          O 
|         / |''',
'''------------
|         | 
|          O 
|         / | 
|          | ''',
'''------------
|         |
|          O 
|         / |
|          |
|         / | 
|
|            ''']

print('Welcome to Hangman! Guess the mystery word with less than 6 mistakes!')


while True:
    words=['table','chair','pencil','stapler','pen','computer','printer','cable','books','shelf']

    alphabet=['a','b','c','d','e','f','g,','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

    number=input('Please enter an integer number (0<=number<10) to choose the word in the list:') 

    if number=='':
        print('Empty input!')
        continue
    elif number in alphabet:
        print('Input must be an integer!')
        continue

    number=int(number)

    if number<0 or number>9:
        print('Index is out of range!')
        continue

    elif 0<=number<10:
        break

words2=[]
words2.extend(words[number])


print('The length of the word is: ',len(words2))

i=0
j=0
x='_'*len(words2)
blankword=[]
blankword.extend(x)

while j<6 and i!=len(words2):
    print('')

    letter=input('Please enter the letter you guess: ')



    if letter in words2:
        print('The letter is in the word.')
        i=i+1

        place=words2.index(letter)
        blankword[place]=letter
        blankword2=''.join(blankword)


        if i==len(words2):
                print('You have found the mystery word. You win!')
                print('Letters matched so far:',blankword2)
                print('Goodbye!')
                break
        else:
            print('Letters matched so far: ',blankword2)
            continue     



    elif letter not in words2:
        if letter not in alphabet:
            print('You need to input a single alphabetic character!')
        elif letter not in words2:
            blankword2=''.join(blankword)
            print('The letter is not in the word.')
            print('Letters matched so far: ',blankword2)
            print(graphics[j])
            j=j+1
            if j==6:
                print('Too many incorrect guesses. You lost!')
                print('The word was:',words[number])
                print('Goodbye!')    

嘿,我做了这个刽子手游戏。我只有一个问题。假设我选择“books”作为首字母 字。如果我输入'o',它只显示图书中的第一个o,而不显示第二个o。我怎样才能得到它 第一次输入字母时要同时显示“o”?在

谢谢!:)


Tags: theinnumberinputifiswordprint
2条回答

我建议使用finditer它的like.find或.replace,除非它能找到你要找的“o”的所有实例。在

str1 = "test"

str2 = "test test test test"

[m.start() for m in re.finditer(str1, str2)]

`#[0, 5, 10, 15]``

而不是:

place=words2.index(letter)
blankword[place]=letter
blankword2=''.join(blankword)

使用:

^{pr2}$

相关问题 更多 >