-
Notifications
You must be signed in to change notification settings - Fork 0
/
project 2 - hangman game.py
55 lines (40 loc) · 1.19 KB
/
project 2 - hangman game.py
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
# Author: Divyanshi Dave
# Python Version: v3.11.2 64-bit
# Import main modules
import random
import time
# Cool little intro thingy i made :)
for i in range(3):
print("Loading game . . .")
time.sleep(1)
print("Welcome to Hangman!")
# words can be run
word_list = ["python", "java", "ruby", "javascript", "html", "css"]
# Select random word from za woord leest
chosen_word = random.choice(word_list)
# init za main vars
guesses = ''
turns = 6
while turns > 0: # MAIN GAME LOOP
# failed attempts go brr
failed = 0
# Display current completion of za word
for letter in chosen_word:
if letter in guesses:
print(letter, end=' ')
else:
print('_', end=' ')
failed += 1
# Check if player guessed all letters
if failed == 0:
print('\nYou won!')
break
guess = input('\nGuess a letter: ').lower()
guesses += guess
# Check if guess in word
if guess not in chosen_word:
turns -= 1
print(f'Wong! You have {turns} turns left.') # Accidentally used f-strings, not avail in python 3.5
if turns == 0:
print('You lose!')
# end main loop when user runs out of turns