[英]I am having a bit of trouble with this hangman program. Can someone help me figure it out?
wanttodo = input('Type 1 if you want rules and 2 if you want to start and 3 if you want to end the program')
while wanttodo!='3':
if wanttodo == '1':
print('Player 1 picks a word (cannot include spaces or dashes) and player two goes guessing letters until the word is complete. You have six chances to get the letters wrong. Player one wins if player 2 doesnt guess it and player 2 wins if he does guess it.')
elif wanttodo=='2':
word = str(input('Player 1: What is your word?'))
word = word.lower()
wordlong = len(word)
rightanswer = False
lives = 6
lettersinword = -1
guessed_letters = list()
nooflettersguessed = len(guessed_letters)
for x in range (100):
print('')
spaces = ('_ '*wordlong)
print(spaces)
while lives>0 and rightanswer == False:
letterguessed = input('Pick a letter. Must not be picked yet.')
letterguessed = letterguessed.lower()
if len(letterguessed)!=1 or not letterguessed.isalpha():
print('Your guess is not valid. Try again.')
elif letterguessed in guessed_letters:
print('Your pick has already been chosen. You have not lost a live. Guess again')
else:
print('You picked a valid and new letter.')
if letterguessed not in word:
print(letterguessed + ' is not in the word.')
lives-=1
elif letterguessed in word:
print('Lovely! ' + letterguessed + ' is in the word.')
for letter in word:
lettersinword+=1
if letter == letterguessed:
listspaces = list(spaces)
listspaces[lettersinword] = letterguessed
spaces = ''.join(listspaces)
print(spaces)
guessed_letters.append(letterguessed)
print(guessed_letters)
When I type in hello as my word, it takes it in properly, when I make my first letter hello, it switches the first underscore to the "h".当我输入 hello 作为我的单词时,它会正确输入,当我输入第一个字母 hello 时,它会将第一个下划线切换为“h”。 But when I type in an 'e', it puts it in the wrong spot, and when I type in 'l', it gives me an error.
但是当我输入“e”时,它会放错位置,当我输入“l”时,它会给我一个错误。 Can someone tell me what the problem is?
有人可以告诉我问题是什么吗?
The problems you describe are in this part of your code:您描述的问题在代码的这一部分中:
lettersinword = -1
while lives>0 and rightanswer == False:
...
for letter in word:
lettersinword+=1
if letter == letterguessed:
listspaces = list(spaces)
listspaces[lettersinword] = letterguessed
spaces = ''.join(listspaces)
When you enter the 'h' in 'hello', lettersinword
is changed to 4
(because it is incremented by 1
for every letter in 'hello'), so when you then enter 'e', lettersinword
is 6
when the statement listspaces[lettersinword] = letterguessed
is run.当你在 'hello' 中输入 'h' 时,
lettersinword
变为4
(因为在 'hello' 中每个字母都加1
),所以当你再输入 'e' 时6
当语句lettersinword
listspaces[lettersinword] = letterguessed
已运行。 You need to reset lettersinword
before each for-loop:您需要在每个 for 循环之前重置
lettersinword
:
while lives>0 and rightanswer == False:
...
lettersinword = -1
for letter in word:
lettersinword+=1
if letter == letterguessed:
listspaces = list(spaces)
listspaces[lettersinword] = letterguessed
spaces = ''.join(listspaces)
There is another error that also needs fixing: spaces
is "h _ _ _ _ "
, so listspaces
becomes ['h', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
.还有另一个需要修复的错误:
spaces
是"h _ _ _ _ "
,所以listspaces
变成['h', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
。 When entering 'e', lettersinword
is now 1
, so listspaces
becomes ['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
, and spaces becomes "he_ _ _ _ "
, not "he _ _ _ "
.输入 'e' 时,
lettersinword
现在是1
,因此listspaces
变为['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
,空格变成"he_ _ _ _ "
,而不是"he _ _ _ "
。 A quick fix would be to change the increment to 2
:一个快速的解决方法是将增量更改为
2
:
lettersinword = -2
for letter in word:
lettersinword+=2
if letter == letterguessed:
listspaces = list(spaces)
listspaces[lettersinword] = letterguessed
spaces = ''.join(listspaces)
However, I would recommend maintaining listspaces
as your list of letters/underscores without spaces separating them, and creating the string from that list every time you want to print it.但是,我建议将列表空间
listspaces
为您的字母/下划线列表,没有空格分隔它们,并在每次要打印时从该列表创建字符串。 Since listspaces
no longer has extra spaces in between letters, the counter can be incremented by 1
instead of 2
each iteration.由于
listspaces
空间在字母之间不再有多余的空格,因此计数器每次迭代都可以增加1
而不是2
。 Also, every time you are iterating through a list and incrementing a counter every iteration, it is better to use the builtin enumerate
.此外,每次迭代列表并每次迭代递增计数器时,最好使用内置的
enumerate
。 Here is a version with those changes:这是具有这些更改的版本:
wanttodo = input('Type 1 if you want rules and 2 if you want to start and 3 if you want to end the program')
while wanttodo != '3':
if wanttodo == '1':
print(
'Player 1 picks a word (cannot include spaces or dashes) and player two goes guessing letters until the word is complete. You have six chances to get the letters wrong. Player one wins if player 2 doesnt guess it and player 2 wins if he does guess it.')
elif wanttodo == '2':
word = str(input('Player 1: What is your word?'))
word = word.lower()
wordlong = len(word)
rightanswer = False
lives = 6
guessed_letters = list()
nooflettersguessed = len(guessed_letters)
for x in range(100):
print('')
listspaces = ['_'] * wordlong
print(' '.join(listspaces)) # replacing: print(spaces)
while lives > 0 and rightanswer == False:
letterguessed = input('Pick a letter. Must not be picked yet.')
letterguessed = letterguessed.lower()
if len(letterguessed) != 1 or not letterguessed.isalpha():
print('Your guess is not valid. Try again.')
elif letterguessed in guessed_letters:
print('Your pick has already been chosen. You have not lost a live. Guess again')
else:
print('You picked a valid and new letter.')
if letterguessed not in word:
print(letterguessed + ' is not in the word.')
lives -= 1
elif letterguessed in word:
print('Lovely! ' + letterguessed + ' is in the word.')
for lettersinword, letter in enumerate(word):
if letter == letterguessed:
listspaces[lettersinword] = letterguessed
spaces = ''.join(listspaces)
print(' '.join(listspaces)) # replacing: print(spaces)
guessed_letters.append(letterguessed)
print(guessed_letters)
PS: in Python, we normally use "snake case" for variable (and function) names, eg letters_guessed
rather than lettersguessed
. PS:在 Python 中,我们通常使用“蛇形大小写”来表示变量(和函数)名称,例如
letters_guessed
而不是lettersguessed
。 It's just what the community is used to reading.这正是社区习惯阅读的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.