#๐Ÿ”’ Is there a better way to do this?

10 messages ยท Page 1 of 1 (latest)

thorny sapphire
#
            if num[index] == 'one':
                num[index] = 1
            elif num[index] == 'two':
                num[index] = 2
            elif num[index] == 'three':
                num[index] = 3
            elif num[index] == 'four':
                num[index] = 4
            elif num[index] == 'five':
                num[index] = 5
            elif num[index] == 'six':
                num[index] = 6
            elif num[index] == 'seven':
                num[index] = 7
            elif num[index] == 'eight':
                num[index] = 8
            elif num[index] == 'nine':
                num[index] = 9
            elif num[index] == 'zero':
sharp ridgeBOT
#

@thorny sapphire

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

glossy marlin
thorny sapphire
#

yes

glossy marlin
#

You can make a dict mapping then

thorny sapphire
#
import re
num_dict = {
        'one':1,
        'two':2,
        'three':3,
        'four':4,
        'five':5,
        'six':6,
        'seven':7,
        'eight':8,
        'nine':9,
        'zero':0
        }
with open('meow.txt','r') as file:
    for line in file:
        num = (re.findall('one|two|three|four|five|six|seven|eight|nine|zero',line))
        for index,item in enumerate(num):
            num[index] == num_dict.get(item)
        print(num)
        
#

nvm got it

#

!close

sharp ridgeBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.