I set 5 tasks to complete 4 of which I did, but the 5th struggle - these are the tasks:
- Create a text file and write a list of 10 English words in it.
- Develop a part of your program that reads words from this text file and selects one randomly.
- Design the part of the program that replaces each letter of the word with the corresponding number in the alphabet. For example, CAT will become 3, 1, 20.
- Design the part of the program that allows the user to enter a number, and then the letter that they think represents. The program should tell them whether it is correct or not, and then display the word with the correct substitutions. For example, if the user enters A for 1, then he should say yes or similar, and then display 3, A, 20.
- Design the part of the program that continues until the user guesses the whole word correctly.
I did before 4, but Im number 5 struggling with my code is this:
import random
import csv
import math
Random_Words = []
alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num = [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]
word_2 = []
words = open('List_Words.txt')
first_10 = []
for line in words:
Random_Words.append(line[:-1])
word = random.choice(Random_Words)
word = list(word)
for character in word:
i = 0
while i < 26:
if character == alph[i]:
word_2.append(num[i])
i = i + 1
print word_2
guess_number = int(raw_input("choose a number: "))
guess_letter = raw_input("choose a letter: ")
i = word.index(guess_letter)
if word_2[i] == guess_number:
print "Correct"
else:
print "Nope"