find number in string, add 1 and replace

12.9k views Asked by At

I need to write a code where a string contains a number, then this number is incremented by 1, and then printed out within the initial string input.

It should work like this

>>>addNumber('I slept 3 hours')
what number would you like to increment? 3
I slept 4 hours
>>>addNumber('I have 366 friends on facebook')
what number would you like to increment? 6
I have 377 friends on facebook

so far I have this, but I know it is wrong and honestly I don't know how to do this.

def incrementNumbers(statement):
    number1=(int(input('What number would you like to increment?')))
    number2 = number1 +1
    i=[]
    for numbers in statement:
        if numbers.isdigit():
            i.append(numbers)
            statement.replace(numbers,number2)
3

There are 3 answers

0
Rakholiya Jenish On BEST ANSWER

Possible error in your code is:

  1. you are trying to replace with int type data in the string directly.
  2. Also only checking numbers.isdigit() will modify all numbers irrespective of what you specified to modify.

Possible corrections to your code to make it run properly:

def incrementNumbers(statement):
    number1 = int(input('What number would you like to increment?'))
    number2 = number1 +1
    i=[]
    for numbers in statement:
        if numbers.isdigit() and int(numbers) == number1:
            i.append(numbers)
            statement.replace(numbers,str(number2))

You can also change your incrementNumbers(statement) function to:

def incrementNumbers(statement):
    number1 = input('What number would you like to increment?')
    number2 = str(int(number1)+1)
    statement = statement.replace(number1,number2)
    return statement

which I think is better than to use a for loop.

0
Bhargav Rao On

You can use re.sub here

>>> import re
>>> s = 'I have 366 friends on facebook'
>>> def addone(obj):
...     val = int(obj.group(1))
...     return str(val+1)
... 
>>> i = raw_input('what number would you like to increment?')
what number would you like to increment?6
>>> re.sub('({})'.format(i),addone,s)
'I have 377 friends on facebook'

Another Example

>>> s = 'I slept 3 hours'
>>> i = raw_input('what number would you like to increment?')
what number would you like to increment?3
>>> re.sub('({})'.format(i),addone,s)
'I slept 4 hours'

And you can define your function like

def addNumber(s):
    def addone(obj):
        val = int(obj.group(1))
        return str(val+1)

    i = raw_input('what number would you like to increment?')
    return(re.sub('({})'.format(i),addone,s))

However I advice that you have a validation function

i = raw_input('what number would you like to increment?')
if 0<int(i)<9:
    return(re.sub('({})'.format(i),addone,s))
else:
    return "Number out of range"

This will prevent the user from entering values outside your range

0
Cory Kramer On

Under your requirements that the numeric value must be between 0 and 8, you can use string.replace

def increment(s, inc):
    return s.replace(str(inc), str(inc + 1))

>>> s = 'I have 366 friends on facebook'
>>> inc = 6
>>> increment(s, 6)
'I have 377 friends on facebook'