0

I am currently having a problem when just reading the number from a string. I somehow does not receive the proper value. I need to distinguish between 1 and 2 numbered strings and need to account for it. My Output looks like the following:

9
1

but it should be:

9
10
def StringToNumber(Period):
    if Period[-1:] == "D":
        period_unit = int(Period[:1])
    elif Period[-1:] == 'M':
        period_unit = int(Period[:1])
    elif Period[-1:] == 'W':
        period_unit = int(Period[:1])
    elif Period[-1:] == 'Y':
        period_unit = int(Period[:1])
    elif Period == '':
        period_unit = int(0)
    else:
        raise Exception('Problems')
    return period_unit

years_string1 = '9Y'
years_string2 = '10Y'

years_number1 = StringToNumber(years_string1)
years_number2 = StringToNumber(years_string2)

print(years_number1)
print(years_number2)
1
  • do it all : period_unit = int(Period[:-1]) Commented Feb 27, 2018 at 13:50

3 Answers 3

1

why don't you try like this, if year_string is in same format

def StringToNumber(Period):
    period_unit = Period[:-1]
    return period_unit

years_string1 = '9Y'
years_string2 = '10Y'

years_number1 = StringToNumber(years_string1)
years_number2 = StringToNumber(years_string2)

print(years_number1)
print(years_number2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. This is what I was looking for.
0

This might help. To get the last element use [-1] instead of [-1:] . And to exclude the last element use [:-1] instead of [:1]

def StringToNumber(Period):
    if Period[-1] == "D":
        period_unit = int(Period[:-1])
    elif Period[-1] == 'M':
        period_unit = int(Period[:-1])
    elif Period[-1] == 'W':
        period_unit = int(Period[:-1])
    elif Period[-1] == 'Y':
        period_unit = int(Period[:-1])
    elif Period == '':
        period_unit = int(0)
    else:
        raise Exception('Problems')
    return period_unit

years_string1 = '9Y'
years_string2 = '10Y'

print(StringToNumber(years_string1))
print(StringToNumber(years_string2))

Output:

9
10

1 Comment

Thanks a lot. Unfortunatly someone was faster than you
0

You need to use a -1 instead of a 1

Try this:

intervals = ['D', 'M', 'W', 'Y']
def StringToNumber(Period):

    if Period[-1:] in intervals:
        period_unit = int(Period[:-1])
    elif Period == '':
        period_unit = int(0)
    else:
        raise Exception('Problems')
    return period_unit

years_string1 = '9Y'
years_string2 = '10Y'

years_number1 = StringToNumber(years_string1)
years_number2 = StringToNumber(years_string2)

print(years_number1)
print(years_number2)

This makes your code a little nicer and decreases redundancy

1 Comment

Thanks a lot. Unfortunatly someone was faster than you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.