1
def is_a_valid_code(code):
    code_letters = ["S", "B", "N", "T", "P"]
    min_for_each_letter = [1, 3, 4, 0, 3] #inclusive
    max_for_each_letter = [7, 9, 6, 7, 5] #inclusive

    first_letter = code[0] #B
    if first_letter.isalpha == False:
        return False
    numbers_part = code[1:]
    numbers_part = numbers_part.strip() #747346
    num_string = ""
    for num in numbers_part:
        if num.isalpha() == True:
            return False
        if num.isdigit() != False:
            num_string = num_string + num

    letter_index = code_letters.index(first_letter)
 

    min_range = min_for_each_letter[letter_index]
    max_range = max_for_each_letter[letter_index]

    for num in range(len(numbers_part)):
        print(int(float(numbers_part[num])))

        
print("1.", is_a_valid_code('B747346'))
print("2.", is_a_valid_code('N  444  454'))
print("3.", is_a_valid_code('T 400 4854'))
print("4.", is_a_valid_code('S  444S454'))
print("5.", is_a_valid_code('P  '))
print("6.", is_a_valid_code('T  0  '))

I am confused why the last 'paragraph' of my code is not working (. I am trying to figure out whether 'num' is greater than the min_range which I calculated earlier. However, I got an error. I read some similar posts and found I needed to convert in into a float first and then an integer, but it still does not work. What is wrong here? The error I get is ValueError: could not convert string to float: ''

Freddy
  • 11
  • 5
  • Where did you read that you need to convert a numeric string into float and then into int? Also, you can't convert a string into a number (float or int) if the string is not numeric. – BoobyTrap May 10 '21 at 23:57
  • https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10 – Freddy May 10 '21 at 23:58
  • @BoobyTrap I'm trying to convert the num part into an int, isn't the num part a numeric string? – Freddy May 11 '21 at 00:00
  • How is the link you posted related to your problem? You are parsing whole integers here, not float values. – Finomnis May 11 '21 at 00:32

1 Answers1

0

the problem is that your loop, in a moment, is trying to convert space character of the string to integer / float. You need to handle the space characters, either before or inside the loop, for the problem not to happen anymore.