I am stuck with a specific task about lists and adding integers and strings

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 Section
import random
import csv
import math

#Start Variables
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 = []

#Foundation Code (List of words)
words = open('List_Words.txt') 
first_10 = []
for line in words:
    Random_Words.append(line[:-1])

#Making the random word 
word = random.choice(Random_Words)
word = list(word)

#Making the random word into an alphabetical numeral
for character in word:
    i = 0 
    while i < 26:
        if character == alph[i]:
            word_2.append(num[i])
        i = i + 1

#print word
#print Random_word
print word_2    

#Letting the person guess the word/ letter   
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"
+3
source share
1 answer
while(1): 
    print word_2  
    #Letting the person guess the word/ letter   
    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:
        word_2[i] = guess_letter # put guessed letter into word_2, for example "3 A 20"
        print "Correct: ", word_2
        if word == word_2:
            break
    else:
        print "Nope"
    print "continue guessing"
+2
source

All Articles