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: ''