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!')
嘿,我做了这个刽子手游戏。我只有一个问题。假设我选择“书籍”作为首字母
字。如果我输入'o',它只显示书中的第一个o而不显示第二个o。我怎么才能得到它
在第一次输入字母时显示'o'?
谢谢! :)
答案 0 :(得分:0)
而不是:
place=words2.index(letter)
blankword[place]=letter
blankword2=''.join(blankword)
使用:
places = []
try:
while True:
place=words2.index(letter)
blankword[place]=letter
blankword2=''.join(blankword)
except IndexError:
答案 1 :(得分:0)
我建议使用像.find或.replace这样的finditer,除非它找到你正在寻找的“o”的alll实例。
str1 = "test"
str2 = "test test test test"
[m.start() for m in re.finditer(str1, str2)]
`#[0,5,10,15]``