How can I correctly change the string into numbers

Question: My goal is to get the word changed into a number, then cut out the middle numbers so its only the first and last term. For example, in my question, the final answer should be 83, but I get 28. The way I did my loop is in the numeric position. How should I fix this?

Repl link: https://replit.com/@Ufoencounter/Advent-Day-1-Part-2#main.py

THE X VARIABLE EQUALS “eighttwothree”

import time
import os
import sys

#Entire Alphabet
alphabet = [
    "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"]
#Empty list, but will append terms to it and add them up in the end.
word_numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
numbers = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9]
number_list = []


#Defining functions
def split(word):
  return list(word)

#Refrence: https://stackoverflow.com/questions/1450897/remove-characters-except-digits-from-string-using-python

def converter():
  global new_message
  global message_list
  message_list = ''.join(filter(str.isdigit, message_list))
  message_list = (split(message_list))
  if len(message_list) == 1:
    message_list.append(message_list[0])
  while len(message_list) > 2:
    if len(message_list) > 2:
      message_list.pop(len(message_list)-(len(message_list)-1))
  new_message = "".join(message_list)
  new_message = int(new_message)
  print(f"The new message is {new_message}!")
  
def main(): 
  global message_list
  global i
  global x
  global message_list
  wordfile = open("test.txt", "r")
  for x in wordfile:
    message_list = []
    i = 0
    while i != 9:
      if word_numbers[i] in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(word_numbers[i], "")
        print(x)
        break
      i += 1
    i = 0
    while i != 9:
      if f"{numbers[i]}" in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(str(numbers[i]), "")
        print(x)
        break
      i += 1
    i = 0
    while i != 9:
      if word_numbers[i] in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(word_numbers[i], "")
        print(x)
        break
      i += 1
    i = 0
    while i != 9:
      if f"{numbers[i]}" in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(str(numbers[i]), "")
        print(x)
        break
      i += 1
    i = 0
    while i != 9:
      if word_numbers[i] in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(word_numbers[i], "")
        print(x)
        break
      i += 1
    i = 0
    while i != 9:
      if f"{numbers[i]}" in x:
        message_list.append(numbers[i])
        print(message_list)
        x = x.replace(str(numbers[i]), "")
        print(x)
        break
      i += 1
    
    message_list = f"{message_list}" + f"{split(x)}"
    converter()
    if new_message == "":
      pass
    else:
      number_list.append(int(new_message))
    time.sleep(1)
    
  print(f"The sum of all terms is {sum(number_list)}")

if __name__ == "__main__":
    main()

1 Like
word_to_number = {
  "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, 
  "six": 6, "seven": 7, "eight": 8, "nine": 9
}

x = "eighttwothree"

for word, number in word_to_number.items():
  x = x.replace(word, str(number))

number = int(x)
result = int(str(number)[0] + str(number)[-1])

print(result)

This should work.

3 Likes