the following codes are for the Hangman game. It gets a type error for the assignment display = list("_" * len(chosen_word))
, saying that it is a Nonetype. Is this because chosen_word is not assigned (which is another error in itself)?. How can it be corrected? Any other suggestion to improve the code is also welcomed.
JavaScript
x
121
121
1
import random
2
3
Dictionary = {"fruits": "watermelon", "buildings": "apartment", "mammal": "horse", "occupation": "fireman"}
4
5
6
def choose_word():
7
hint, chosen_word = random.choice(list(Dictionary.items()))
8
print("Hint: " + hint)
9
blank = []
10
for letter in chosen_word:
11
blank.append("_")
12
print("".join(blank))
13
return chosen_word
14
15
16
def draw_hangman(attempt):
17
stages = [ # final state: head, torso, both arms, and both legs
18
"""
19
--------
20
| |
21
| O
22
| \|/
23
| |
24
| / \
25
-
26
""",
27
# head, torso, both arms, and one leg
28
"""
29
--------
30
| |
31
| O
32
| \|/
33
| |
34
| /
35
-
36
""",
37
# head, torso, and both arms
38
"""
39
--------
40
| |
41
| O
42
| \|/
43
| |
44
|
45
-
46
""",
47
# head, torso, and one arm
48
"""
49
--------
50
| |
51
| O
52
| \|
53
| |
54
|
55
-
56
""",
57
# head and torso
58
"""
59
--------
60
| |
61
| O
62
| |
63
| |
64
|
65
-
66
""",
67
# head
68
"""
69
--------
70
| |
71
| O
72
|
73
|
74
|
75
-
76
""",
77
# initial empty state
78
"""
79
--------
80
| |
81
|
82
|
83
|
84
|
85
-
86
"""
87
]
88
return stages[attempt]
89
90
91
def play_hangman(chosen_word):
92
attempt = 6
93
guessed = False
94
guessed_letters = []
95
display = list("_" * len(chosen_word))
96
while attempt > 0 and not guessed:
97
print(draw_hangman(attempt))
98
player_guess = input("nPlease guess a letter between A-Zn")
99
letter = 0
100
if len(player_guess) == 1 and player_guess.isalpha() and player_guess in chosen_word:
101
while chosen_word.find(player_guess, letter) != -1:
102
letter = chosen_word.find(player_guess, letter)
103
display[letter] = player_guess
104
letter += 1
105
guessed_letters.append(player_guess)
106
print("".join(display))
107
if display[letter] == chosen_word:
108
guessed = True
109
print("Congratulation, you won!")
110
elif player_guess in guessed_letters:
111
print("You have already guessed this letter")
112
else:
113
print("Your guess is not valid")
114
attempt -= 1
115
print(draw_hangman(attempt))
116
else:
117
print("You ran out of attempts")
118
119
120
play_hangman(choose_word())
121
Error 1 (resolved):
JavaScript
1
7
1
Traceback (most recent call last):
2
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 119, in <module>
3
play_hangman(choose_word())
4
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 94, in play_hangman
5
display = list("_" * len(chosen_word))
6
TypeError: object of type 'NoneType' has no len()
7
Error 2 : after a few guesses, index gets of range
JavaScript
1
10
10
1
Traceback (most recent call last):
2
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 120, in <module>
3
play_hangman(choose_word())
4
File "/Users/owly/PycharmProjects/pythonProject1/main12.py", line 107, in play_hangman
5
if display[letter] == chosen_word:
6
IndexError: list index out of range
7
w__er_e__n
8
9
Process finished with exit code 1
10
Advertisement
Answer
Following code worked perfectly,
JavaScript
1
6
1
str1 = ''.join(display)
2
if str1 == chosen_word:
3
guessed = True
4
print("Congratulation, you won!")
5
attempt=-1
6
I have just added above code and some minor changes in if-else conditions and some print statements. Remove extra print statements if you want!
Full code-
JavaScript
1
127
127
1
import random
2
Dictionary = {"fruits": "watermelon", "buildings": "apartment", "mammal": "horse", "occupation": "fireman"}
3
4
5
def choose_word():
6
hint, chosen_word = random.choice(list(Dictionary.items()))
7
print("Hint: " + hint)
8
blank = []
9
for letter in chosen_word:
10
blank.append("_")
11
print("".join(blank))
12
return chosen_word
13
14
15
def draw_hangman(attempt):
16
stages = [ # final state: head, torso, both arms, and both legs
17
"""
18
--------
19
| |
20
| O
21
| \|/
22
| |
23
| / \
24
-
25
""",
26
# head, torso, both arms, and one leg
27
"""
28
--------
29
| |
30
| O
31
| \|/
32
| |
33
| /
34
-
35
""",
36
# head, torso, and both arms
37
"""
38
--------
39
| |
40
| O
41
| \|/
42
| |
43
|
44
-
45
""",
46
# head, torso, and one arm
47
"""
48
--------
49
| |
50
| O
51
| \|
52
| |
53
|
54
-
55
""",
56
# head and torso
57
"""
58
--------
59
| |
60
| O
61
| |
62
| |
63
|
64
-
65
""",
66
# head
67
"""
68
--------
69
| |
70
| O
71
|
72
|
73
|
74
-
75
""",
76
# initial empty state
77
"""
78
--------
79
| |
80
|
81
|
82
|
83
|
84
-
85
"""
86
]
87
return stages[attempt]
88
89
90
def play_hangman(chosen_word):
91
attempt = 6
92
guessed = False
93
guessed_letters = []
94
display = list("_" * len(chosen_word))
95
96
while attempt > 0:
97
if not guessed:
98
print(draw_hangman(attempt))
99
player_guess = input("nPlease guess a letter between A-Zn")
100
letter = 0
101
if len(player_guess) == 1 and player_guess.isalpha() and player_guess in chosen_word and player_guess not in guessed_letters:
102
while chosen_word.find(player_guess, letter) != -1:
103
print("running while:"+str(letter))
104
letter = chosen_word.find(player_guess, letter)
105
display[letter] = player_guess
106
letter += 1
107
guessed_letters.append(player_guess)
108
109
print("".join(display))
110
print(display)
111
str1 = ''.join(display)
112
if str1 == chosen_word:
113
guessed = True
114
print("Congratulation, you won!")
115
attempt=-1
116
elif player_guess in guessed_letters:
117
print("You have already guessed this letter")
118
else:
119
print("Your guess is not valid")
120
attempt -= 1
121
print(draw_hangman(attempt))
122
else:
123
print("You ran out of attempts")
124
125
126
play_hangman(choose_word())
127
let me know if there is any difficulty!!