yoyo
yoyo

Reputation: 25

How do i convert a string into an integer?

So for my activity, I want to check if a car number is valid.
To validate a given vehicle plate number with 4 digits starting with ‘S’, the following must be done:
• Ignore the letter ‘S’
• Assign a number to the letters as follows: A=1, B=2, C=3, …etc. For the number plate SBA 1234, this is converted to 21 1234
• The 6 individual numbers need to be multiplied by 9,4,5,4,3,2 respectively and added together. For the example above, we will get, 2 x 9 + 1 x 4 + 1 x 5 + 2 x 4 + 3 x 3 + 4 x 2 = 52

At the moment I managed to do the first two points and replace the letters with numbers, but for the part where I need to multiply the numbers respectively, I believe python still reads it(Newnum) as a string so I need to convert it to an integer so they can be multiplied.

vehicle_num = input("Enter the vehicle number to be validated: ")
if "S" in vehicle_num:
    Newnum = vehicle_num.replace("S","").replace("A","1").replace("B","2").replace("C","3")
    print(Newnum)
else:
    print("Not working")
multiplynum = Newnum

multiplynum = ((Newnum[0] * 9) + (Newnum[1] * 4) + (Newnum[2] * 5) +
(Newnum[3] * 4) + (Newnum[4] * 3) + (Newnum[5] * 2))
print(Newnum)

Upvotes: 0

Views: 294

Answers (6)

alani
alani

Reputation: 13079

In answer to the specific question, you would use int(value) for the type conversion.

As an aside, you might also consider using a regular expression to validate whether your vehicle number has the expected format and to extract the relevant data items from it. For example:

import re

vehicle_num = "SBA1234"
multipliers = [9, 4, 5, 4, 3, 2]

m = re.match("S(?P<letters>[A-Z]{2}) *(?P<digits>[0-9]{4})$", vehicle_num)

if m:
    values = ([ord(char) - ord('A') + 1 for char in m.group('letters')] +
              [int(char) for char in m.group('digits')])

    print(sum(mult * value for mult, value in zip(multipliers, values)))

else:
    print('Does not match the expected pattern')

Upvotes: 0

Benedictanjw
Benedictanjw

Reputation: 838

I'm writing this answer in a way that you could get some other benefit from it apart from just this problem! This solution doesn't catch all errors and can be much improved but I guess you just need a starting point.

vehicle_num = input("Enter the vehicle number to be validated: ")

trimmed_vnum = [n for n in vehicle_num if n not in ['S', ' ']] # list comprehension, ternary operator
letter_to_num = {'A': 1, 'B': 2, 'C': 3} # dictionary

converted_num = []
for n in trimmed_vnum:
    if n in letter_to_num:
        n = letter_to_num[n]
    else:
        n = int(n) # explicit type conversion
    converted_num.append(n)

multiplier = [9,4,5,4,3,2]

if len(converted_num) == len(multiplier):
    final = sum([a*b for a,b in zip(converted_num, multiplier)]) # zip, list comprehension

print(final)

Upvotes: 0

a_guest
a_guest

Reputation: 36249

You can use various functions and methods to make it more concise:

from string import ascii_uppercase

def convert(s, *, factors=(9, 4, 5, 4, 3, 2)):
    letters, numbers = s.split()
    letters = letters[1:]  # ignore prefix 'S'
    table = {x: i for i, x in enumerate(ascii_uppercase, start=1)}
    letters = [table[x] for x in letters]  # replace letters with numbers
    numbers = [int(x) for x in numbers]
    return sum(x*y for x, y in zip(letters + numbers, factors))

Upvotes: 0

Abhishek J
Abhishek J

Reputation: 2584

ord will for here. Because it converts the character to its ascii equivalent. We then subtract enough so that A=1, B=2 etc.

def solve(vehicle_num):
    if vehicle_num[0] != 'S':
        return 'Not Working'
    vehicle_num = [ord(x) - 64 if not x.isdigit() else int(x) for x in vehicle_num[1:].replace(' ', '')]
    return sum([x * y for x, y in zip([9, 4, 5, 4, 3, 2], vehicle_num)])


print(solve('SBA 1234'))

#Output
52

Upvotes: 0

William Clavier
William Clavier

Reputation: 334

Like this:

vehicle_num = input("Enter the vehicle number to be validated: ")
if "S" in vehicle_num:
    Newnum = vehicle_num.replace("S","").replace("A","1").replace("B","2").replace("C","3")
    print(Newnum)
else:
    print("Not working")

Newnum = list(map(int, Newnum))

multiplynum = ((Newnum[0] * 9) + (Newnum[1] * 4) + (Newnum[2] * 5) +
(Newnum[3] * 4) + (Newnum[4] * 3) + (Newnum[5] * 2))
print(Newnum)

Upvotes: 0

Yeahprettymuch
Yeahprettymuch

Reputation: 541

Simply int(your_string) should work, if your string doesn't have any other special characters.

Upvotes: 3

Related Questions