Home > database >  How do I iterate through a list of numbers in string format?
How do I iterate through a list of numbers in string format?

Time:03-09

This is my code:

def interpretCashier(char):
    allnumber = False
    num_list = ['1','2','3','4','5','6','7','8','9']
    chara = '.'
    mixed = False
    i = 0
    for x in num_list:
        if x in char:
            allnumber = True
        else:
            mixed = True
            nextchara = char[i   1]
        i  = 1
        if allnumber is True and mixed is False and chara not in char:
            return "PIN"
        elif mixed is True and chara in char and nextchara != '.':
            return 'Transaction'
        else:
            return 'Password'

    print(interpretCashier("25.79"))
    print(interpretCashier("123456"))
    print(interpretCashier("12!@#@!@32"))

The above should print Transaction, PIN, and Password.

my output is Transaction, PIN and PIN

CodePudding user response:

There are 2 issues in this code

  • The reason why you are not getting PIN instead of PASSWORD for last test case is due to indentation issue.
  • There is another issue in this program, you are not cheking the index range of incoming string inside the forloop which will result in index out of bounds error.

Here is the fixed code

def interpretCashier(char):
    allnumber = False
    num_list = ['1','2','3','4','5','6','7','8','9']
    chara = '.'
    mixed = False
    i = 0
    
    for x in num_list:
        if i 1<len(char):
            if x in char:
                allnumber = True
            else:
                mixed = True
                nextchara = char[i   1]
            i  = 1
        
    if allnumber is True and mixed is False and chara not in char:
        return "PIN"
    elif mixed is True and chara in char and nextchara != '.':
        return 'Transaction'
    else:
        return 'Password'

print(interpretCashier("25.79"))
print(interpretCashier("123456"))
print(interpretCashier("12!@#@!@32"))

CodePudding user response:

You could us simple conditional statements to determine which category the input argument belongs to.

def interpreCashier(text):
    # checks if the str can be interpreted as an int or float
    if text.isnumeric():
        return "PIN"
    if "." in text:
        return "Transaction"
    return "Password"

Addressing the question in the title though, you can iterate a string of numbers the same way you iterate a list of numbers.

text = "12367854"
for n in text:
    (do something)

One of the issues with your code is that the return statements are on the inside of the for loop. This example shows how to iterate the input string, and apply the filtering logic.

def interpretCashier(char):
    num_list = ['1','2','3','4','5','6','7','8','9']
    chara = '.'
    mixed = False
    i = 0
    for c in char:
        if c not in num_list:
            mixed = True
    if mixed is False:
        return "PIN"
    if mixed is True and chara in char:
        return 'Transaction'
    else:
        return 'Password'
  • Related