-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayWordle.py
More file actions
86 lines (69 loc) · 2.57 KB
/
playWordle.py
File metadata and controls
86 lines (69 loc) · 2.57 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import random
infile = open("outputLength4.txt", "r")
allText = infile.read()
words = list(allText.split('\n'))
infile.close()
def getWord(): ##choosing a random word
word = random.choice(words)
return word.upper()
def play(word):
wordCompletion = "_"*len(word)
guessed = False
guessedLetters = []
guessedWords = []
tries = 10
## counters = {}
print(wordCompletion)
print("\n")
while not guessed and tries > 0:
guess = input("Please, guess a letter or word: ").upper()
if len(guess) == 1 and guess.isalpha():
if guess in guessedLetters:
print("You already guessed the letter", guess)
elif guess not in word:
print(guess, "is not in the word. You lost a life.")
tries -= 1
guessedLetters.append(guess)
else:
print("Good job! ", guess, " is the word.")
guessedLetters.append(guess)
wordAsList = list(wordCompletion)
wordListt = list(word)
## for i in wordListt:
## if i not in counters:
## counters[i]=1
## else:
## counters[i] += 1
indices = [i for i, letter in enumerate(word) if letter == guess] ##attributing an index to each value in the list
for i in indices:
wordAsList[i] = guess
wordCompletion = "".join(wordAsList)
if "_" not in wordCompletion:
guessed = True
elif len(guess) == len(word) and guess.isalpha():
if guess in guessedWords:
print("You already guessed the word.", guess)
elif guess != word:
print(guess, "is not in the word. You lost a life.")
tries -= 1
guessedWords.append(guess)
else:
guessed = True
wordCompletion = word
else:
print("Invalid guess")
tries -= 1
print("You have ",tries,"tries left.")
print(wordCompletion)
print("\n")
if guessed == True:
print("Congratulations!")
else:
print("Sorry, you ran out of tries. The word was ", word)
def main():
word = getWord()
play(word)
while input("Play again? Y or N").upper() == "Y":
word = getWord()
play(word)
main()