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','penney','computer','printer','cable','america','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:
increment=0
print('The letter is in the word.')
i=i+1
while words2.count(letter)!=blankword.count(letter):
place=words2.index(letter,increment)
blankword[place]=letter
blankword2=''.join(blankword)
i=i+1
increment=increment+1
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!')
break
结果是我得到的是。在
^{pr2}$嗨。这是我制作的python hangman游戏,但每次运行时都会出现这个错误。我不知道这个错误意味着什么。有人知道是什么导致了这个错误吗?否则效果很好。 谢谢!在
PS公司
这可能不是唯一的问题,但在您发布的示例中,尽管“n”是“penney”的一部分,但仍得到
ValueError
的原因是因为您从未重置increment
,并且当您对字符串调用index
时,它被用作搜索的开始。在由于您在第二次尝试时输入了“e”,因此增量将等于3,并且从那里开始,它将永远找不到“n”。在
这是你的原始代码的一小部分,我修改了它,使它可以工作。我去掉了增量。在
相关问题 更多 >
编程相关推荐