Search This Blog

Learning Python : Day 03

 











Phase 1:


#Step 1

word_list = ["aardvark", "baboon", "camel"]

chosen_word=random.choice(word_list)
print(chosen_word)

guess=input("Guess the Letter:").lower()

j=0
for i in chosen_word:
if i == guess:
j+=1
print(f'right: {j}')
else:
print('wrong')


Phase 2: Replace the underscores/blanks with the matched letter

display=[]

for i in chosen_word:
display+='_'
guess = input("Guess a letter: ").lower()
j=0
for letter in chosen_word:
if letter == guess:
display[j]=letter
j+=1
else:
j+=1
print(display)

Second Solution:

word_length=len(chosen_word)
display=[]
for _ in range(word_length):
display+="_"

for pos in range(word_length):
letter=chosen_word[pos]
if letter==guess:
display[pos]=letter

print(display)


Output:


Pssst, the solution is aardvark.
Guess a letter: a
['a', 'a', '_', '_', '_', 'a', '_', '_']

Phase 3: Guess until all the _ of the display are replaced by the chosen word and end the game

end_of_game=False
while(end_of_game!=True):
guess = input("Guess a letter: ").lower()

#Check guessed letter
for position in range(word_length):
letter = chosen_word[position]
if letter == guess:
display[position] = letter
print(display)
if '_' not in display:
end_of_game=True
print('You win')

Output:

Pssst, the solution is baboon.
Guess a letter: b
['b', '_', 'b', '_', '_', '_']
Guess a letter: a
['b', 'a', 'b', '_', '_', '_']
Guess a letter: b
['b', 'a', 'b', '_', '_', '_']
Guess a letter: o
['b', 'a', 'b', 'o', 'o', '_']
Guess a letter: n
['b', 'a', 'b', 'o', 'o', 'n']
You win