-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAlpha_hangman game.py
More file actions
65 lines (56 loc) · 2.46 KB
/
Copy pathCodeAlpha_hangman game.py
File metadata and controls
65 lines (56 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Create a simple text-based Hangman game where the player guesses a word one letter at a time.
Simplified Scope:
● Use a small list of 5 predefined words (no need to use a file or API).
● Limit incorrect guesses to 6.
● Basic console input/output — no graphics or audio.
Key Concepts Used: random, while loop, if-else, strings, lists.
"""
#library for selecting random item from the list
import random
guessword=['snake','conda','tiger','horse','popee']
word = random.choice(guessword)
#list created to store user successful attemptsn
game_board = ["_","_","_","_","_"]
print("\n\nHellooo Player! Let's Have some fun!")
#print('The hangman word IS:',word)
# here i is for counting and 0 is its base value
# f_c is also a variablle created as a base for allowed number of attempts
i=0
f_c=6
#loop that sets limit of attempts
# so that it runs until the either the user succeedes of failes available attempts
while f_c>0 :
#this is a variable that collects the items of game board
#as one string and compares it to the random word
compare_s="".join(game_board)
#the program checks if there is a match
if compare_s == word:
print('congrats!, you won!')
break
tryin=input("\n put your letter of choice here -------> ")
print(tryin)
# this is for making sure of appropriate input
if len(tryin) == 1 :
# a boolean variable that will determine what will be running next
found_match=False
#loop here to go runs only if index is
for index in range(len(word)):
if word[index] == tryin :
game_board[index] = tryin
#change in boolean state will run the following condition:
found_match = True
#i is increasing in value only if the user is choosing a right character
i=i+1
if found_match==True:
print(f"Success! You picked: {tryin}")
print("your word now is :----->",game_board)
#here if condition is false the the range of attemps left will decrease
elif found_match==False:
f_c=f_c -1
print('wrong,',f_c,"attemps left")
#conditions to make sure tht user input is appropriate
elif len(tryin)>1:
print("Error: You must type exactly ONE character.Try again\n")
else:
print('Error:invalid input, try something different\n')