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". 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. 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. You need to reset lettersinword
before each for-loop:
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', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
. When entering 'e', lettersinword
is now 1
, so listspaces
becomes ['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' ']
, and spaces becomes "he_ _ _ _ "
, not "he _ _ _ "
. A quick fix would be to change the increment to 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. Since listspaces
no longer has extra spaces in between letters, the counter can be incremented by 1
instead of 2
each iteration. Also, every time you are iterating through a list and incrementing a counter every iteration, it is better to use the builtin 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
. It's just what the community is used to reading.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.